Remove duplicates from Sorted List II

Source: Internet
Author: User

      • Title Description
      • Link Address
      • Solution

Title Description

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

Example

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

Link Address

http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/

Solution
ListNode*Deleteduplicates (ListNode*Head) {//Write your code here        if(Head== NULL) {returnHead } ListNode*Dummynode= NewListNode (); Dummynode -Next=Head ListNode*Or:=Dummynode; ListNode*Cur=Or: -Next while(cur!= NULL &&Cur -Next!= NULL) {if(cur -Val==Cur -Next -Val) {ListNode*Temp=Cur while(Temp!= NULL &&Temp -Next!= NULL &&Temp -Val==Temp -Next -Val) {ListNode*Del=Temp -Next Temp -Next=Del -Next                Delete del; } Pre -Next=Temp -Next Cur=Pre -Next            Delete temp; }Else{Pre=Cur Cur=Cur -Next }        }returnDummynode -Next }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Remove duplicates from 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: 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.