首页 > python教程

python/golang 删除链表中的元素

时间:2020-09-22 python教程 查看: 891

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

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 链表的资料请关注python博客其它相关文章!

展开全文
上一篇:通过代码实例了解Python sys模块
下一篇:Python logging日志库空间不足问题解决
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下