黑松山资源网 Design By www.paidiu.com
先用使用常规方法,两个指针:
golang实现:
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 { // 要删除的是头节点 link.head = current.next } else if current == link.tail { // 要删除的是尾节点 previous.next = nil link.tail = previous } else { // 要删除的是中间的节点 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实现:
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: # 要删除的是头节点 self.head = cur.next elif cur == self.tail: # 要删除的是尾节点 pre.next = None self.tail = pre else: # 要删除的是中间的节点 pre.next = cur.next self.lenth -= 1 break pre = cur cur = cur.next if cur == None: print("未找到", v) break
只使用使用一个指针实现链表的删除:
golang实现:
func (link *Link) remove_with_one_pointer(v int) { if link.lenth == 0 { return } if link.tail.value == v { // 要删除的节点是尾节点,需特殊处理 if link.lenth == 1 { // 如果链表只有一个节点 link.head = nil link.tail = nil } else { //大于一个节点 cur := link.head for ; cur.next.next != nil; cur = cur.next { } //找到尾节点的前一个节点 cur.next = nil link.tail = cur } link.lenth -= 1 return } //要删除的节点在头部/中间 的常规情况 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实现:
def remove_with_one_pointer(self, v): if self.lenth == 0: return if self.tail.value == v: # 要删除的节点是尾节点,需特殊处理 if self.lenth == 1: # 如果链表只有一个节点 self.head = None self.tail = None else: # 大于一个节点 cur = self.head while True: if cur.next.next is None: # 找到尾节点的前一个节点 break else: cur = cur.next cur.next = None self.tail = cur self.lenth -= 1 return # 要删除的节点在头部/中间 的常规情况 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 删除链表中的元素的详细内容,更多关于python/golang 链表的资料请关注其它相关文章!
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月05日
2024年10月05日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]