python/golang 刪除鏈表中的元素
先用使用常規(guī)方法,兩個指針:
golang實現(xiàn):
type Node struct { value int next *Node } type Link struct { head *Node tail *Node lenth int } // 向鏈表中添加元素 func (link *Link) add(v int) { if link.lenth == 0 { // 當前鏈表是空鏈表 link.head = &Node{v, nil} link.tail = link.head link.lenth = 1 } else { newNond := &Node{v, nil} link.tail.next = newNond link.tail = newNond link.lenth += 1 } } // 刪除鏈表中的元素(雙指針) func (link *Link) remove(v int) { if link.lenth == 0 { fmt.Println("空鏈表,不支持該操作") return } var previous *Node = nil for current := link.head; current != nil; current = current.next { if current.value == v { if current == link.head { // 要刪除的是頭節(jié)點 link.head = current.next } else if current == link.tail { // 要刪除的是尾節(jié)點 previous.next = nil link.tail = previous } else { // 要刪除的是中間的節(jié)點 previous.next = current.next } link.lenth -= 1 break } previous = current } } // 打印鏈表 func (link *Link) printList() { if link.lenth == 0 { fmt.Println("空鏈表") return } for cur := link.head; cur != nil; cur = cur.next { fmt.Printf("%d ", cur.value) } fmt.Println() }
python實現(xiàn):
class Node: def __init__(self, value, next): self.value = value self.next = next def __str__(self): return str(self.value) class Link: def __init__(self): self.head = None self.tail = None self.lenth = 0 # 向鏈表中添加元素 def add(self, v): if self.lenth == 0: # 當前鏈表是空鏈表 self.head = Node(v, None) self.tail = self.head self.lenth = 1 else: new_node = Node(v, None) self.tail.next = new_node self.tail = new_node self.lenth += 1 # 打印鏈表 def print(self): if self.lenth == 0: print('空鏈表') return cur = self.head while True: if cur == None: print() break print(cur, end=' ') cur = cur.next # 刪除鏈表中的元素 def remove(self, v): if self.lenth == 0: return cur = self.head pre = None while True: if cur.value == v: if cur == self.head: # 要刪除的是頭節(jié)點 self.head = cur.next elif cur == self.tail: # 要刪除的是尾節(jié)點 pre.next = None self.tail = pre else: # 要刪除的是中間的節(jié)點 pre.next = cur.next self.lenth -= 1 break pre = cur cur = cur.next if cur == None: print("未找到", v) break
只使用使用一個指針實現(xiàn)鏈表的刪除:
golang實現(xiàn):
func (link *Link) remove_with_one_pointer(v int) { if link.lenth == 0 { return } if link.tail.value == v { // 要刪除的節(jié)點是尾節(jié)點,需特殊處理 if link.lenth == 1 { // 如果鏈表只有一個節(jié)點 link.head = nil link.tail = nil } else { //大于一個節(jié)點 cur := link.head for ; cur.next.next != nil; cur = cur.next { } //找到尾節(jié)點的前一個節(jié)點 cur.next = nil link.tail = cur } link.lenth -= 1 return } //要刪除的節(jié)點在頭部/中間 的常規(guī)情況 for cur := link.head; cur != nil; cur = cur.next { if cur.value == v { cur.value = cur.next.value cur.next = cur.next.next link.lenth -= 1 return } } fmt.Println("未找到", v) }
python實現(xiàn):
def remove_with_one_pointer(self, v): if self.lenth == 0: return if self.tail.value == v: # 要刪除的節(jié)點是尾節(jié)點,需特殊處理 if self.lenth == 1: # 如果鏈表只有一個節(jié)點 self.head = None self.tail = None else: # 大于一個節(jié)點 cur = self.head while True: if cur.next.next is None: # 找到尾節(jié)點的前一個節(jié)點 break else: cur = cur.next cur.next = None self.tail = cur self.lenth -= 1 return # 要刪除的節(jié)點在頭部/中間 的常規(guī)情況 cur = self.head while True: if cur.value == v: cur.value = cur.next.value cur.next = cur.next.next self.lenth -= 1 break cur = cur.next if cur is None: print('未找到', v) break
以上就是python/golang 刪除鏈表中的元素的詳細內(nèi)容,更多關(guān)于python/golang 鏈表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pycharm 使用心得(八)如何調(diào)用另一文件中的函數(shù)
事件環(huán)境: pycharm 編寫了函數(shù)do() 保存在make.py 如何在另一個file里調(diào)用do函數(shù)?2014-06-06Python MOCK SERVER moco模擬接口測試過程解析
這篇文章主要介紹了Python MOCK SERVER moco模擬過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04基于Python-turtle庫繪制路飛的草帽骷髏旗、美國隊長的盾牌、高達的源碼
這篇文章主要介紹了基于Python-turtle庫繪制路飛的草帽骷髏旗、美國隊長的盾牌、高達的源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02python如何求取指定范圍內(nèi)的質(zhì)數(shù)
這篇文章主要介紹了python如何求取指定范圍內(nèi)的質(zhì)數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Python利用ElementTree模塊處理XML的方法詳解
ElementTree是python的XML處理模塊,它提供了一個輕量級的對象模,下面這篇文章就來給大家介紹了關(guān)于Python利用ElementTree模塊處理XML的方法,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-08-08Python實現(xiàn)將doc轉(zhuǎn)化pdf格式文檔的方法
這篇文章主要介紹了Python實現(xiàn)將doc轉(zhuǎn)化pdf格式文檔的方法,結(jié)合實例形式分析了Python實現(xiàn)doc格式文件讀取及轉(zhuǎn)換pdf格式文件的操作技巧,以及php調(diào)用py文件的具體實現(xiàn)方法,需要的朋友可以參考下2018-01-01