/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* p = head;
if(head == NULL){
return head;
}
ListNode* q = head->next;
if( p != NULL){
while(q !=NULL){
if(q->val == p->val){
p->next = q->next;
}
else{
//p->next = q;
//这个时候p的next是指向q的list不会断
p = q;
//q = p->next;
}
//q每次都是需要向后移动的
q = q->next;
}
return head;
}
else{
return head;
}
}
};
83. Remove Duplicates from Sorted List
最新推荐文章于 2023-01-13 08:10:00 发布