Simple c++ solution


  • 0
    N
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
           if (head == NULL)
    			return head;
    		ListNode h(-1);
    		h.next = head;
    		ListNode *cur = head;
    		ListNode *prev = &h;
    		while (cur->next != NULL) {
    			if (cur->val == cur->next->val) {
    				while (cur->next != NULL && cur->val == cur->next->val) {
    					cur = cur->next;
    				}
    				cur = cur->next;
    				prev->next = cur;
    				if(cur==NULL)
    				{
    					break;
    				}
    			}else{
    				prev = cur;
    				cur = cur->next;
    			}
    		}
    		return h.next;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.