Leetcode–remove duplicates from Sorted List II (Java)

Source: Internet
Author: User

Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the Original list.

For example,
Given 1->2->3->3->4->4->5 , return 1->2->5 .
Given 1->1->1->2->3 , return 2->3 .

Problem Solving Ideas:

In contrast to I, this problem requires that no duplicate nodes be retained. Therefore, an index record is required at the end of the node that is currently reserved, and then traversed with another node, and the duplicate values and new values are processed in the process.

The difficulty lies in the treatment of corner cases. Not long after the beginning of the brush, have not developed the habit of pre-written test, so in these special cases, spent a lot of time.

Corner Case: [1,2,2],[0,1,2,2,3,4],[1,1],[1],[1,1,2,2]

1         if(head!=NULL){2ListNode C1 =NewListNode (head.val-1);3C1.next =head;4ListNode re = c1, C2 =head;5              intCount = 0, temp =C1.next.val;6              7               while(c2!=NULL){8                  if(Temp!=c2.val && count>1){9C1.next =C2; Tentemp =C2.val; OneCount = 1; A}Else if(Temp!=c2.val && Count==1){ -C1 =C1.next; -temp =C2.val; theCount = 1; -}Else  -Count + +; -C2 =C2.next;               +          } -              if(Count > 1) c1.next =NULL; +              returnRe.next;          A      } at          return NULL;

Leetcode–remove duplicates from Sorted List II (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.