劍指offer 刪除鍊錶中重複的結點

2021-10-08 19:49:46 字數 578 閱讀 7436

在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。 例如,鍊錶1->2->3->3->4->4->5 處理後為 1->2->5

題解:借助輔助頭結點,可避免單獨討論頭結點的情況。設定兩個結點 pre 和 cur,當 cur 和 cur.next 值相等,cur 一直向前走,直到不等退出迴圈,這時候 cur 指的值還是重複值,調整 cur 和 pre 的指標再次判斷。

public

class

solution

listnode head =

newlistnode

(integer.min_value)

; head.next = phead;

listnode pre = head;

listnode cur = phead;

while

(cur != null)

cur = cur.next;

pre.next = cur;

}else

}return head.next;

}}

劍指offer 鍊錶 刪除鍊錶中的重複節點

題目在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 思路分析 思路一 使用linkedlist儲存不重複節點,重構鍊錶 分析評價 這個方法是乙個比較直接且容易想到的方法,使用時只要注意一些情況...

劍指offer 刪除鍊錶中重複的結點(鍊錶)

在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 class solution listnode ans newlistnode 1 ans next phead listnode link a...

劍指Offer 鍊錶 刪除鍊錶中重複的結點

題目 在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5。注意該鍊錶為排序鍊錶,重複的節點不保留哦!public class listnode 1 遞迴 遞迴的方法就像是先判斷第乙個節點和之後的節...