企業(yè)網(wǎng)站維護的要求包括/聚名網(wǎng)域名
. - 力扣(LeetCode)
給定一個已排序的鏈表的頭?head
?,?刪除原始鏈表中所有重復數(shù)字的節(jié)點,只留下不同的數(shù)字?。返回?已排序的鏈表?。
示例 1:
輸入:head = [1,2,3,3,4,4,5] 輸出:[1,2,5]
示例 2:
輸入:head = [1,1,1,2,3] 輸出:[2,3]
提示:
- 鏈表中節(jié)點數(shù)目在范圍?
[0, 300]
?內(nèi) -100 <= Node.val <= 100
- 題目數(shù)據(jù)保證鏈表已經(jīng)按升序?排列
class Solution {
public:ListNode* deleteDuplicates(ListNode* head) {if (!head) {return head;}ListNode* dummy = new ListNode(0, head);ListNode* cur = dummy;while (cur->next && cur->next->next) {if (cur->next->val == cur->next->next->val) {int x = cur->next->val;while (cur->next && cur->next->val == x) {cur->next = cur->next->next;}}else {cur = cur->next;}}return dummy->next;}
};