0
1

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.

asked 22 Apr '12, 14:40

1337c0d3r's gravatar image

1337c0d3r ♦♦
1.2k582171
accept rate: 2%


123next »

ListNode *deleteDuplicates(ListNode *head) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    ListNode *p=head,*q;
    if (p == NULL || p->next == NULL) return p;
    q = p->next;
    // invariant: no duplication in range [head, p] && q == p->next
    while (q != NULL)  // reach the end, loop terminates
    {
        if (q->val != p->val)   
        {
            // find a new val, advance p to the next, new [head,p] has no duplicates.
            p=p->next;  
            // make sure q==p->next, and move closer to loop end
            q=q->next;
        }
        else
        {
            // find duplicated value, range [head,p] is not changed.
            ListNode *t=q;
            // advance q to move closer to loop end, make sure q == p->next
            q=q->next;
            p->next = q;            
            // avoid memory leak.
            delete t;
        }

    }
    return head;

}
link

answered 12 Jan, 13:12

roger's gravatar image

roger
664
accept rate: 0%

edited 12 Jan, 13:38

1

@roger: Welcome to the community! I like your code (and others you had posted), it is very elegant. Could you please consider improving your answers with one or two sentences describing your code? That way people will take notice and vote up your elegant solutions :)

(12 Jan, 13:17) 1337c0d3r ♦♦ 1337c0d3r's gravatar image

ListNode deleteDuplicates(ListNode head) {

    if(!head) return NULL;
    ListNode *prev = head, *cur = head;        
    while(cur->next) {
        cur = cur->next;            
        if(prev->val==cur->val) {
            prev->next = cur->next;
            delete cur;
            cur = prev;                
        }    
        else 
            prev = cur;            
    }
    return head;
}

};

link

answered 23 Jun, 08:04

varunvats32's gravatar image

varunvats32
213
accept rate: 0%

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (!head) return NULL;
        ListNode *pre = head, *cur = head->next;
        while (cur) {
            if (cur->val == pre->val) {
                pre->next = cur->next;
                delete cur;
                cur = pre;
            }
            pre = cur;
            cur = cur->next;
        }

        return head;
    }
};
link

answered 19 Jan, 06:22

Linfuzki's gravatar image

Linfuzki
53856
accept rate: 0%

public ListNode deleteDuplicates(ListNode head) {
    ListNode root = new ListNode(-1);
    ListNode last = root, cur_node = head;
    while (cur_node != null) {
        ListNode next_node = cur_node.next;
        while (next_node != null && next_node.val == cur_node.val) {
            next_node = next_node.next;
        }
        last.next = cur_node;
        last = cur_node;
        last.next = null;
        cur_node = next_node;
    }
    return root.next;
}
link

answered 21 Jan, 17:44

lvlv's gravatar image

lvlv
864
accept rate: 0%

if( !head) return NULL;
ListNode * cur = head;
ListNode * nxt = head->next;
while( nxt)
{
    if( cur->val == nxt->val)
        {
            ListNode *tmp = nxt;
            cur->next = nxt->next;
            nxt = cur->next;
            delete tmp;
        }
    else
        {
            cur = nxt;
            nxt = nxt->next;
        }
}
return head;
link

answered 22 Jan, 19:18

spooky's gravatar image

spooky
124
accept rate: 0%

give you a comment!

(23 Jan, 05:13) BlackMamba BlackMamba's gravatar image
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(!head)
    return NULL;
ListNode* pRead = head->next;
ListNode* pWrite = head;
while (pRead)
{
    while (pRead && pRead->val == pWrite->val)
        pRead = pRead->next;
    pWrite->next = pRead;
    pWrite = pRead;
}
return head;
}
};
link

answered 23 Jan, 05:12

BlackMamba's gravatar image

BlackMamba
11124
accept rate: 0%

public class Solution {
public ListNode deleteDuplicates(ListNode head) {
    // Start typing your Java solution below
    // DO NOT write main() function
    if (head==null) return head;
    ListNode curr = head.next;
    ListNode prev = head;
    while (curr != null) {
        while ((curr != null) && (curr.val == prev.val) ) {
            curr = curr.next;
        }
        prev.next = curr;
        if (curr == null) break;
        prev = prev.next;
        curr = curr.next;
    }
    return head;
}
}
link

answered 06 Feb, 13:24

jjlights's gravatar image

jjlights
102
accept rate: 0%

ListNode *deleteDuplicates(ListNode *head) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if(!head)
        return head;
    if(!head->next)
        return head;

    ListNode *curr = head->next, *pre = head;

    while(curr) {

        if(curr->val == pre->val) {
            ListNode *rem = curr;
            pre->next = curr->next;
            curr = pre->next;
            delete rem;
        }
        else {
            pre = curr;
            curr = curr->next;
        }
    }
    return head;
}
link

answered 12 Feb, 19:01

Shengzhe%20Chen's gravatar image

Shengzhe Chen
1124
accept rate: 4%

public class Solution {
public ListNode deleteDuplicates(ListNode head) {
    // Start typing your Java solution below
    // DO NOT write main() function
    if(head==null) return head;
    ListNode prev=head;
    ListNode cur=head.next;

    while(cur!=null){
        if(cur.val!=prev.val) prev=prev.next;
        cur=cur.next;
        prev.next=cur;
    }
    return head;
}

}

link

answered 27 Feb, 17:40

Nathan's gravatar image

Nathan
344
accept rate: 0%

ListNode *deleteDuplicates(ListNode *head) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if (!head || !head->next) {
        return head;
    }

    ListNode *hd, *nx;
    hd = head;
    nx = hd->next;

    for (; nx != NULL;nx = nx->next) {
        if (hd->val == nx->val) {
            hd->next = nx->next;
        } else {
            hd = hd->next;
        }
    }

    return head;
}
link

answered 31 Mar, 14:41

whitebluff's gravatar image

whitebluff
212
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • Indent code by 4 spaces.
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×139

Asked: 22 Apr '12, 14:40

Seen: 2,183 times

Last updated: yesterday