Delete duplicate values in a single linked list (Java edition)

Source: Internet
Author: User

Title: Delete Elements of duplicate values in a single linked list of lead nodes (that is, there is only one element for each value)

Problem Solving Ideas:

With a dynamic secondary storage array, each time you put an element into a secondary array, let the length of the auxiliary array add 1, the longest time is as long as a single linked list, set a pointer p to point to the head node, start with the first element in a single linked list, place its value in a secondary array, and then access the elements behind the single Compare the value of the element with the value of all the elements in the array that are already assigned, if not equal to the value of any element in the array, let P's next point to the next pointer of the node, delete the node element, otherwise p to the next pointer to P, and place the value of the currently accessed node element into the auxiliary array, Always access the last element in a single-linked list.


ADT Definition:

Single-linked list of node class lnode{//to simplify access to a single-linked list, the data entry in the node is set to Publicpublic int data;public lnode next;}

Algorithm implementation:

public class Linklistutli {public static void Deleterepeatvalue (Lnode L) {//If the single-linked list is empty, or if there is only a header node, or if there is only one element behind the head node. There is no possibility of duplicate values if (l==null| | l.next==null| | L.next.next==null) return; Lnode p=l;//The previous node of the current access node Lnode q=l.next;//the currently visited node int count=0;while (q!=null) {count++;} int temp[]=new int[count];count=1;temp[count-1]=q.data;while (q.next!=null)//Go down until you access the last node in the single-linked list {q=q.next;// Accesses the next node, Boolean flag=false;//, to mark whether the value in the current access node element is the same as the value of the previous access node element for (int i=0;i<count;i++) {if (Q.data==temp[i]) {P.next =q.next;flag=true;break;}} if (!flag) {count++;temp[count-1]=q.data;q=q.next;//Let Q go one step further to ensure that P is the previous node of the next Access node}}q=null;//at this point the Q may be a pointer that has been released, If it does not point to null, it becomes a wild pointer}}


Delete duplicate values in a single linked list (Java edition)

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.