python/golang 删除链表中的元素

作者:NothingLeft了 时间:2021-05-05 14:08:07 

先用使用常规方法,两个指针:

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

只使用使用一个指针实现链表的删除:

python/golang 删除链表中的元素

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

来源:https://www.jianshu.com/p/8554d637702a

标签:python,golang,删除,链表
0
投稿

猜你喜欢

  • 浅析python参数的知识点

    2022-12-16 01:39:32
  • Python+Seaborn绘制分布图的示例详解

    2021-08-14 16:51:29
  • 详解element-ui 表单校验 Rules 配置 常用黑科技

    2023-08-17 17:42:47
  • python使用cv2库、下载opencv库的方法

    2022-09-05 00:45:09
  • python如何在循环引用中管理内存

    2023-12-20 13:18:40
  • 手把手带你了解Python数据分析--matplotlib

    2022-05-21 22:07:18
  • asp如何实现无组件上传二进制文件?

    2010-06-03 10:09:00
  • python子类如何继承父类的实例变量

    2022-05-07 08:41:26
  • Python字典创建 遍历 添加等实用基础操作技巧

    2021-05-23 22:22:31
  • python 实时调取摄像头的示例代码

    2021-10-17 06:07:52
  • Django自定义User模型、认证、权限控制的操作

    2022-08-03 16:06:17
  • AJAX打造博客无刷新搜索

    2007-08-23 08:48:00
  • 彻底解决MySql在UTF8字符集下乱码问题

    2011-06-02 12:02:00
  • PHP使用redis实现分布式锁的示例详解

    2023-06-01 16:32:19
  • Python3.6 之后字典是有序的?

    2021-02-14 08:27:53
  • python队列Queue的详解

    2022-10-09 16:56:21
  • 关于Python形参打包与解包小技巧分享

    2021-06-08 21:43:39
  • python字符串的多行输出的实例详解

    2021-06-25 20:59:45
  • sqlserver 多表查询不同数据库服务器上的表

    2012-04-13 11:41:51
  • Python 使用 Pillow 模块给图片添加文字水印的方法

    2022-06-13 04:13:37
  • asp之家 网络编程 m.aspxhome.com