Delete duplicate numbers in a sorted list II

Source: Internet
Author: User


Given a sorted list, deleting all duplicate elements leaves only the elements in the original linked list that are not duplicates.
Sample Example
Give 1->2->3->3->4->4->5->null, return 1->2->5->null

Give 1->1->1->2->3->null, return 2->3->null


Record the same number
One of the same save, multiple skipped

/** * Definition for ListNode * public class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * next = NULL; *     } * } */ Public  class solution {    /** * @param ListNode Head is the head of the linked list * @return: ListNode head of the LI nked list * /     Public StaticListNodedeleteduplicates(ListNode head) {//Write your code hereListNode Newhead =NewListNode (0);if(Head = =NULL|| = =NULL)returnHead        ListNode cur = newhead; ListNode prev = head; while(head!=NULL) {ListNode p = head;intCount =0;//Record the number of identical nodes             while(p!=NULL&& P.val = = head.val) {p =;            count++; }if(count==1){//1 instructions on = head;                cur =;            head =; }Elsehead = p; } =NULL;//Disconnect the back node; }}

Delete duplicate numbers in a sorted list II

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: 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.