python/golang 删除链表中的元素


Posted in Python onSeptember 14, 2020

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

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

以上就是python/golang 删除链表中的元素的详细内容,更多关于python/golang 链表的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python的高级Git库 Gittle
Sep 22 Python
Python的ORM框架中SQLAlchemy库的查询操作的教程
Apr 25 Python
用pickle存储Python的原生对象方法
Apr 28 Python
Python入门之三角函数atan2()函数详解
Nov 08 Python
TensorFlow平台下Python实现神经网络
Mar 10 Python
解决python3 pika之连接断开的问题
Dec 18 Python
python利用Opencv实现人脸识别功能
Apr 25 Python
Python数据类型之Tuple元组实例详解
May 08 Python
ipython和python区别详解
Jun 26 Python
Python3标准库之dbm UNIX键-值数据库问题
Mar 24 Python
django 解决扩展自带User表遇到的问题
May 14 Python
Python调用飞书发送消息的示例
Nov 10 Python
Python基于pillow库实现生成图片水印
Sep 14 #Python
python/golang实现循环链表的示例代码
Sep 14 #Python
python实现canny边缘检测
Sep 14 #Python
Python gevent协程切换实现详解
Sep 14 #Python
通过实例了解python__slots__使用方法
Sep 14 #Python
python如何遍历指定路径下所有文件(按按照时间区间检索)
Sep 14 #Python
详解python实现可视化的MD5、sha256哈希加密小工具
Sep 14 #Python
You might like
浅析ThinkPHP中execute和query方法的区别
2014/06/13 PHP
php实现源代码加密的方法
2015/07/11 PHP
php Session无效分析资料整理
2016/11/29 PHP
PHP空值检测函数与方法汇总
2017/11/19 PHP
Yii框架中用response保存cookie,用request读取cookie的原理解析
2019/09/04 PHP
jQuery选择没有colspan属性的td的代码
2010/07/06 Javascript
用JQuery实现表格隔行变色和突出显示当前行的代码
2012/02/10 Javascript
javascript event在FF和IE的兼容传参心得(绝对好用)
2014/07/10 Javascript
详解JavaScript数组的操作大全
2015/10/19 Javascript
Javascript实现跑马灯效果的简单实例
2016/05/31 Javascript
微信小程序商城项目之淘宝分类入口(2)
2017/04/17 Javascript
ES6 javascript的异步操作实例详解
2017/10/30 Javascript
基于jQuery实现定位导航位置效果
2017/11/15 jQuery
微信小程序使用request网络请求操作实例
2017/12/15 Javascript
Vue修改mint-ui默认样式的方法
2018/02/03 Javascript
微信小程序购物车、父子组件传值及calc的注意事项总结
2018/11/14 Javascript
node实现简单的增删改查接口实例代码
2019/08/22 Javascript
Vue+ElementUI 中级联选择器Bug问题的解决
2020/07/31 Javascript
关于Node.js中频繁修改代码重启服务器的问题
2020/10/15 Javascript
Python实现在Linux系统下更改当前进程运行用户
2015/02/04 Python
Python字符串替换实例分析
2015/05/11 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
2016/03/30 Python
Python环境搭建之OpenCV的步骤方法
2017/10/20 Python
使用Python实现从各个子文件夹中复制指定文件的方法
2018/10/25 Python
Python基于机器学习方法实现的电影推荐系统实例详解
2019/06/25 Python
简单了解Django应用app及分布式路由
2019/07/24 Python
pytorch 固定部分参数训练的方法
2019/08/17 Python
pycharm 2018 激活码及破解补丁激活方式
2020/09/21 Python
python不到50行代码完成了多张excel合并的实现示例
2020/05/28 Python
计算机应用专业应届毕业生中文求职信范文
2013/11/29 职场文书
升职感谢信
2015/01/22 职场文书
音乐课外活动总结
2015/05/09 职场文书
贫困证明书范文
2015/06/16 职场文书
关于感恩的歌曲整理(8首)
2019/08/14 职场文书
Canvas三种动态画圆实现方法说明(小结)
2021/04/16 Javascript
springboot 自定义配置 解决Boolean属性不生效
2022/03/18 Java/Android