程式設計訓練第六十六期 刪除排序鍊錶中的重複元素

2021-10-25 20:54:56 字數 760 閱讀 7795

給定乙個排序鍊錶,刪除所有重複的元素,使得每個元素只出現一次。

1.遍歷

時間複雜度o(n)

空間複雜度o(1)

/**

* definition for singly-linked list.

* struct listnode

* listnode(int x) : val(x), next(nullptr) {}

* listnode(int x, listnode *next) : val(x), next(next) {}

* };

*/class

solution

else

}return head;}}

;

乙個指標完成遍歷

時間複雜度o(n)

空間複雜度o(1)

/**

* definition for singly-linked list.

* struct listnode

* listnode(int x) : val(x), next(nullptr) {}

* listnode(int x, listnode *next) : val(x), next(next) {}

* };

*/class

solution

else

}return head;}}

;

鍊錶 刪除排序鍊錶中的重複元素

一 遍歷方法 思路 通過遍歷一遍鍊錶在遍歷的過程中刪除節點,因為鍊錶是有序的,所以只需比較當前節點和當前節點的下乙個節點,這裡需要注意一下,如果有重複的使用了head.next head.next.next刪除下乙個節點了,但是於下乙個新的節點對比還是現在這個節點,所以不要head head.nex...

刪除排序鍊錶中的重複元素 鍊錶

今天我們開始另一模組的演算法題,關於鍊錶的操作.let s do it 題目 給定乙個排序鍊錶,刪除所有重複的元素,使得每個元素只出現一次.示例1 輸入 1 1 2 輸出 1 2 示例2 輸入 1 1 2 3 3 輸出 1 2 3 思想 public class listnode func dele...

刪除排序鍊錶中的重複元素

給定乙個排序鍊錶,刪除所有重複的元素每個元素只留下乙個。樣例 給出 1 1 2 null,返回 1 2 null 給出 1 1 2 3 3 null,返回 1 2 3 null 加乙個頭結點 鍊錶有序,找到不相等的時候,刪除中間結點 兩兩比較是否相同進行刪除 definition for listn...