3->1->5->5->7,去掉重復(fù)項(xiàng)后變?yōu)椋?->3->5->7 方法: 順序刪除 遞歸刪除 1.順序刪..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python無(wú)序鏈表刪除重復(fù)項(xiàng)的方法

發(fā)布時(shí)間:2020-10-09 16:26:39 來(lái)源:腳本之家 閱讀:131 作者:布?xì)W不歐 欄目:開(kāi)發(fā)技術(shù)

題目描述:

給定一個(gè)沒(méi)有排序的鏈表,去掉重復(fù)項(xiàng),并保留原順序 如: 1->3->1->5->5->7,去掉重復(fù)項(xiàng)后變?yōu)椋?->3->5->7

方法:

  1. 順序刪除
  2. 遞歸刪除

1.順序刪除

由于這種方法采用雙重循環(huán)對(duì)鏈表進(jìn)行遍歷,因此,時(shí)間復(fù)雜度為O(n**2)
在遍歷鏈表的過(guò)程中,使用了常數(shù)個(gè)額外的指針變量來(lái)保存當(dāng)前遍歷的結(jié)點(diǎn),前驅(qū)結(jié)點(diǎn)和被刪除的結(jié)點(diǎn),所以空間復(fù)雜度為O(1)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time  : 2020/1/15 20:55
# @Author : buu
# @Software: PyCharm
# @Blog  :https://blog.csdn.net/weixin_44321080
class LNode:
  def __init__(self, x):
    self.data = x
    self.next = None

def removeDup(head):
  """
  對(duì)帶頭結(jié)點(diǎn)的無(wú)序單鏈表刪除重復(fù)的結(jié)點(diǎn)
  順序刪除:通過(guò)雙重循環(huán)直接在鏈表上進(jìn)行刪除操作
  即,外層循環(huán)用一個(gè)指針從第一個(gè)結(jié)點(diǎn)開(kāi)始遍歷整個(gè)鏈表,內(nèi)層循環(huán)從外層指針指向的下一個(gè)結(jié)點(diǎn)開(kāi)始,
  遍歷其余結(jié)點(diǎn),將與外層循環(huán)遍歷到的的指針?biāo)傅慕Y(jié)點(diǎn)的數(shù)據(jù)域相同的結(jié)點(diǎn)刪除
  :param head: 頭指針
  :return:
  """
  if head is None or head.next is None:
    return
  outerCur = head.next
  innerCur = None
  innerPre = None
  while outerCur is not None:
    innerCur = outerCur.next
    innerPre = outerCur
    while innerCur is not None:
      if outerCur.data == innerCur.data:
        innerPre.next = innerCur.next
        innerCur = innerCur.next
      else:
        innerPre = innerCur
        innerCur = innerCur.next
    outerCur = outerCur.next

if __name__ == '__main__':
  i = 1
  head = LNode(6)
  tmp = None
  cur = head
  while i < 7:
    if i % 2 == 0:
      tmp = LNode(i + 1)
    elif i % 3 == 0:
      tmp = LNode(i - 2)
    else:
      tmp = LNode(i)
    cur.next = tmp
    cur = tmp
    i += 1
  print("before removeDup:")
  cur = head.next
  while cur is not None:
    print(cur.data, end=' ')
    cur = cur.next
  removeDup(head)
  print("\nafter removeDup:")
  cur = head.next
  while cur is not None:
    print(cur.data, end=' ')
    cur = cur.next

結(jié)果:

python無(wú)序鏈表刪除重復(fù)項(xiàng)的方法

2.遞歸

此方法與方法一類似,從本質(zhì)上而言,由于這種方法需要對(duì)鏈表進(jìn)行雙重遍歷,所以時(shí)間復(fù)雜度為O(n**2)
由于遞歸法會(huì)增加許多額外的函數(shù)調(diào)用,所以從理論上講,該方法效率比方法一低

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time  : 2020/1/15 21:30
# @Author : buu
# @Software: PyCharm
# @Blog  :https://blog.csdn.net/weixin_44321080
class LNode:
  def __init__(self, x):
    self.data = x
    self.next = None
def removeDupRecursion(head):
  """
  遞歸法:將問(wèn)題逐步分解為小問(wèn)題,即,對(duì)于結(jié)點(diǎn)cur,首先遞歸地刪除以cur.next為首
  的子鏈表中重復(fù)的結(jié)點(diǎn);接著刪除以cur為首的鏈表中的重復(fù)結(jié)點(diǎn),
  :param head:
  :return:
  """
  if head.next is None:
    return head
  pointer = None
  cur = head
  head.next = removeDupRecursion(head.next)
  pointer = head.next
  while pointer is not None:
    if head.data == pointer.data:
      cur.next = pointer.next
      pointer = cur.next
    else:
      pointer = pointer.next
      cur = cur.next
  return head
def removeDup(head):
  """
  對(duì)帶頭結(jié)點(diǎn)的單鏈表刪除重復(fù)結(jié)點(diǎn)
  :param head: 鏈表頭結(jié)點(diǎn)
  :return:
  """
  if head is None:
    return
  head.next = removeDupRecursion(head.next)
if __name__ == '__main__':
  i = 1
  head = LNode(6)
  tmp = None
  cur = head
  while i < 7:
    if i % 2 == 0:
      tmp = LNode(i + 1)
    elif i % 3 == 0:
      tmp = LNode(i - 2)
    else:
      tmp = LNode(i)
    cur.next = tmp
    cur = tmp
    i += 1
  print("before recursive removeDup:")
  cur = head.next
  while cur is not None:
    print(cur.data, end=' ')
    cur = cur.next
  removeDup(head)
  print("\nafter recurseve removeDup:")
  cur = head.next
  while cur is not None:
    print(cur.data, end=' ')
    cur = cur.next

結(jié)果:

python無(wú)序鏈表刪除重復(fù)項(xiàng)的方法

引申:從有序鏈表中刪除重復(fù)項(xiàng)

上述介紹的方法也適用于鏈表有序的情況,但是由于上述方法沒(méi)有充分利用到鏈表有序這個(gè)條件,因此,算法的性能肯定不是最優(yōu)的。本題中,由于鏈表具有有序性,因此不需要對(duì)鏈表進(jìn)行兩次遍歷。所以有如下思路:
用cur指向鏈表的第一個(gè)結(jié)點(diǎn),此時(shí)需要分為以下兩種情況討論:

  • 如果cur.data == cur.next.data,則刪除cur.next結(jié)點(diǎn);
  • 如果cur.data != cur.next.data,則cur=cur.next,繼續(xù)遍歷其余結(jié)點(diǎn);

總結(jié)

以上所述是小編給大家介紹的python無(wú)序鏈表刪除重復(fù)項(xiàng)的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI