"Leetcode" Remove duplicates from Sorted List in JAVA

Source: Internet
Author: User

Given a sorted linked list, delete all duplicates such this each element appear only once.

For example,
Given 1->1->2 , return 1->2 .

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

The idea is very easy. Because the good sort, is to infer that the next is not bigger than it is better, assuming large, then skip the next direct link to the next next. But notice at this time. Consider the assumption that this is the case of 1->1->1. When you delete the second 1. The pointer must remain at the first position so that the ability to infer the 1 is equal to the next 1 (that is, the first 1 and the 3rd 1).

The only thing that needs extra attention is the last two, because you're going to p.next=p.next.next to delete it, so for the last two next that doesn't exist. So it's just as good as null.

Package Testandfun;public class Deleteduplicates {public static void main (String args[]) {deleteduplicates DP = new DeleteD Uplicates (); ListNode head = new ListNode (3); ListNode p1 = new ListNode (3); head.next=p1; ListNode P2 = new ListNode (3);p 1.next = p2; ListNode p3 = new ListNode (3);p 2.next = p3; ListNode P4 = new ListNode;p 3.next = P4;prinf (head);p Rinf (Dp.deletedup (Head));} private static void Prinf (ListNode input) {while (input!=null) {System.out.print (input.val+ "); input = Input.next;} System.out.println ();} Public ListNode Deletedup (ListNode head) {if (head==null| | Head.next==null) return head;//if No head, what is should I do? ListNode p=head;int i=0;//system.out.println (p.val+ "and" +p.next.val); while (p.next! = null) {if (p.val==p.next.val &&p.next.next!=null) {//system.out.println ("go First" +p.val);//"^^" +p.next.val+ "percent" +p.next.next.val); P.next=p.next.next;continue;//if this and next equal, we should stay in the case Next.next is equal this}else if (p.val ==p.next.val&&P.next.next==null) {//system.out.println ("go Second" +p.val);p. next=null;continue;} System.out.println (p.val+ "Round" +i++);p =p.next;if (p==null) break;} System.out.print (Head.val); return head;}}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

"Leetcode" Remove duplicates from Sorted List in JAVA

Related Article

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.