LeetCode 83. Remove Duplicates from Sorted List
程序员文章站
2022-07-14 08:40:46
...
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
给定一个已排序的链表,删除其中重复的结点。
思路:
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == NULL) return head;
ListNode **pp = &head;
int preValue = (*pp)->val;
pp = &((*pp)->next);
while (*pp)
{
int value = (*pp)->val;
if (preValue == value)
{
ListNode *p = *pp;
*pp = (*pp)->next;
delete p;
}
else {
preValue = value;
pp = &((*pp)->next);
}
}
return head;
}
};
推荐阅读
-
【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
-
LeetCode 83. Remove Duplicates from Sorted List
-
83. Remove Duplicates from Sorted List
-
LeetCode 83. Remove Duplicates from Sorted List
-
LeetCode 83. Remove Duplicates from Sorted List ***
-
Leetcode 83. Remove Duplicates from Sorted List
-
Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)
-
26. Remove Duplicates from Sorted Array
-
【LeetCode】80. Remove Duplicates from Sorted Array II (删除排序数组中的重复项 II)-C++实现及详细图解
-
【LeetCode】26. Remove Duplicates from Sorted Array (删除排序数组中的重复项)-C++实现的两种方法