用指標cur、pre分邊儲存當前結點和當前結點的前乙個結點。
通過cur遍歷整個鍊錶,cur順著這個節點向後搜尋,直到遇到有重複的數字,而pre總是指向cur的前乙個結點
建立乙個頭節點相連,是考慮了第乙個值也是重複項
# -*- coding:utf-8 -*-
# class listnode:
# def __init__(self, x):
# self.val = x
# self.next = none
class solution:
def deleteduplication(self, phead):
# write code here
newhead = listnode('0')
newhead.next = phead
pre, cur = none, newhead
while cur:
pre = cur
cur = cur.next
while cur and cur.next and cur.next.val == cur.val:
val = cur.val
while cur and cur.val == val:
cur = cur.next
pre.next = cur
return newhead.next
刪除重複結點(鍊錶)
在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 題目 tpid 13 tqid 11209 rp 1 ru activity oj qru ta coding interviews quest...
刪除鍊錶中重複的結點
題目描述 在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5。刪除鍊錶中重複的結點 author 過路的守望 public class duplicationnode 新建乙個節點指向頭結點 li...
刪除鍊錶中重複的結點
在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 解法一 遞迴 public class listnode public class solution if phead.next.val phe...