Delete duplicate nodes in a linked list

Source: Internet
Author: User

The topic is described in a sorted list, where there are duplicate nodes, delete the duplicate nodes in the linked list, duplicate nodes are not retained, and return the chain header pointers. For example, the list 1->2->3->3->4->4->5 is treated as 1->2->5Public class Solution {Public ListNode deleteduplication (listnode phead){ListNode first = new ListNode (-1);//Set up a predecessor node for the head junction .first.next = phead;ListNode p = phead;ListNode last = first;//assign this predecessor node toWhile (P! = null && P.next = null) {if (p.val = = p.next.val) {int val = p.val;While (P! = null && val = = p.val)p = p.next;//If the values of the current and next nodes are the same, p continues to move backwardslast.next = p;             }else {Last = P;p = p.next;            }        }return first.next;    }}

Delete duplicate nodes in a linked 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.