Delete the number of duplicates in the sequence linked list (1) leecode

Source: Internet
Author: User

Given a sorted Linked List, delete all duplicates such that each element appear onlyOnce.

For example,
Given1->1->2, Return1->2.
Given1->1->2->3->3, Return1->2->3.

1/** 2 * definition for singly-linked list. 3 * Public class listnode {4 * int val; 5 * listnode next; 6 * listnode (int x) {7 * val = x; 8 * Next = NULL; 9 *} 10 *} 11 */12 public class solution {13 public listnode deleteduplicates (listnode head) {14 15 if (Head = NULL) return head; 16 if (head. next = NULL) return head; 17 18 listnode pre = head; // Save the pre to delete19 listnode cur = head. next; 20 while (Cur! = NULL) 21 {22 if (cur. val = pre. val) 23 {24 25 pre. next = cur. next; // Delete the cur node 26 cur = cur. next; // process the next node 27 28 29} 30 else31 {32 pre = pre. next; 33 cur = cur. next; 34 35} 36 37 38} 39 40 return head; 41 42 43} 44}

 

 

 

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.