Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created March 31, 2017 17:44
Show Gist options
  • Save s4553711/a2576dcc0c34de42750f83ac532a7798 to your computer and use it in GitHub Desktop.
Save s4553711/a2576dcc0c34de42750f83ac532a7798 to your computer and use it in GitHub Desktop.
leetcode 83. pay attention to the second while loop which could be result in null ListNode struct so NULL check is required.
/**
* 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) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode* node = head;
while(node != NULL && node->next != NULL) {
while (node->next != NULL && node->next->val == node->val) {
ListNode* n = node->next;
node->next = node->next->next;
delete n;
}
node = node->next;
}
return head;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment