【LeetCode】83. Remove Duplicates from Sorted List
问题描述
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==0) return head;
ListNode* p = head;
ListNode* q = p->next;
while(q) {
if(p->val == q->val) {
p->next = q->next;
q = p->next;
} else {
p = q;
q = q->next;
}
}
return head;
}
};