s4553711
3/31/2017 - 5:44 PM

leetcode 83. pay attention to the second while loop which could be result in null ListNode struct so NULL check is required.

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;
    }
};