16th & 17 remove duplicates from sorted list

Source: Internet
Author: User

Remove duplicates from sorted list I

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

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

Solution:
<span style="font-weight: normal;">/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode deleteDuplicates(ListNode head) {        if(head==null || head.next==null) return head;        ListNode prev = head, cur = head.next;        while(cur!=null){            if(prev.val==cur.val){//remove cur from the list                prev.next=cur.next;                 cur=cur.next;            }            else{                prev=prev.next;                cur=cur.next;            }        }        return head;            }}</span>

Remove duplicates from sorted List II

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

For example,
Given1->2->3->3->4->4->5, Return1->2->5.
Given1->1->1->2->3, Return2->3.

<span style="font-size:18px;">/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode deleteDuplicates(ListNode head) {        if(head==null || head.next==null) return head;        ListNode newHead = new ListNode(0);//create a new empty head, the value of the new head will not be counted in        newHead.next=head;        ListNode prev = newHead, cur=head;        while(cur!=null&&cur.next!=null){            if(cur.val!=cur.next.val){                prev=prev.next;                cur=cur.next;            }else{                while(cur.next!=null&&cur.next.val==cur.val)    cur=cur.next;                //remove nodes from prev.next to cur                prev.next=cur.next;                cur=cur.next;            }        }        return newHead.next;    }}</span>
Note that the while condition must be cur! = NULL and cur. Next! = NULL. When only one element is left in the traversal list (cur. Next = NULL), it is clear that there is no possibility of equality.

16th & 17 remove duplicates from sorted list

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.