Summary of linked list problems

Source: Internet
Author: User

    • Linked list

  A linked list is a basic one-dimensional discrete storage data structure. Relative to the array, it is discrete memory, can not be the same as the subscript to query the corresponding data, each linked list node can only know its previous (double-ended linked list) and its next (single linked list, double-ended linked list) node. C Language Control memory through Malloc/free, C + + through New/delete,java is only the new object.

1.Java Linked list node definition

Class node{private node Next;private int data;public node () {}public node (int data) {This.data=data;} public int GetData () {return data;} public void SetData (int data) {this.data = data;} Public Node GetNext () {return next;} public void Setnext (Node next) {This.next = Next;}}

2. List Operation class

Class Linkedlist{private node First;public node GetFirst () {return first;} public void Setfirst (Node first) {This.first = first;} Searches for the first node equal to the target by value, public node findNode (int target) {if (first==null) {System.out.println ("initialise"); return null;} Node P=first;while (P.getdata ()!=target && p!=null) P=p.getnext (); return p;} Public node Findprenode (node target) {if (first==null) {System.out.println ("initialise the First"); return null;} Node P=first;while (P.getnext ()!=target) P=p.getnext (); return p;} Returns the list subscript lookup public int indexofnode (int target) {int index=1;if (first==null) {System.out.println ("initialise the first"); return-1;} Node P=first;while (P.getdata ()!=target) {index++;p =p.getnext (); if (p==null) {index=-1;break;}} return index;} At the end of the Add node public void AddNode (node add) {if (first==null) {first=add;add.setnext (null); return;} Node P=first;while (P.getnext ()!=null) P=p.getnext ();p. Setnext (add); Add.setnext (null);} Header interpolation generates a list of public void Headadd (node node) {if (first = = null) {first=node;node.setnext (null);}Else{node.setnext (first); first=node;}} Delete node--the node in the list must be specified public void Deletenode (node Todel) {if (first==null) {System.out.println ("initialise the first"); return;} Node P=todel.getnext (), if (p!=null) {Todel.setnext (P.getnext ()), Todel.setdata (P.getdata ());p. Setnext (null);} Else{deleteendnode (Todel);}} Delete End public void Deleteendnode (node Todel) {node p=first;while (p.getnext ()!=todel && p!=null) p=p.getnext (); P.setnext (null);} General delete public void Deletenormal (node Todel) {node P=findprenode (todel);p. Setnext (Todel.getnext ()); Todel.setnext (null );} Modify the value of a node public void Update (node Target,int value) {if (first==null) {System.out.println ("initialise the first"); return;} Target.setdata (value);} public void Printlist () {if (first==null) {System.out.println ("initialise the first"); Node P=first;while (p!=null) {System.out.print (P.getdata () + "");p =p.getnext ();} System.out.println ();}}

Small Note: (1) The generation of single-linked list can have head interpolation and tail interpolation method, the addition of the tail interpolation node is O (n) complex, delete is O (n); the head interpolation is O (1), delete or O (n).

(2) The deletion method has an O (1) complexity. Method: Given a node, delete its next node and transfer the value of the next node to the given node.

    • Issue 1: Delete duplicate nodes in the list. (Data duplication)

It was a bit strange to write because of the method that I used to delete the O (1). The overall approach is to use an O (n) space to store the nodes, find duplicates to delete, and traverse the time complex to use only O (n).

Delete Unordered list duplicate Node 1 buffer version public void DelRepeated1 (LinkedList list) {hashmap<integer,boolean> buffer=new hashmap< Integer,boolean> (); Node P=list.getfirst (); while (P!=null) {System.out.println ("Data=" +p.getdata ()); if (Buffer.get (P.getdata ()) ==null) {Buffer.put (P.getdata (), true);} Else{list.deletenode (P); while (true) {if (Buffer.get (P.getdata ())!=null) List.deletenode (p); elsebreak;}} P=p.getnext ();}}

If you do not use buffers, you can only use the violent calendar, starting from the current node to the footer "eliminationist", "Step by Step". Complexity O (n*n).

And the Delete method is using the O (n) complexity.

Delete unsorted two tables repeating node 2 no buffer public void DelRepeated2 (LinkedList list) {node P = list.getfirst (); Node q = List.getfirst (). GetNext (), while (p!=null) {q = P.getnext (), while (Q!=null) {if (P.getdata () ==q.getdata ()) List.deletenormal (q); Q=q.getnext ();} p = P.getnext ();}}
    • Question 2: Look for the lowest-count K nodes.

    This problem is actually quite simple, if the list is self-built, then add a length or size property records so it is much easier to solve the problem. If only a single linked list is given, then the index of the inverted k is calculated by traversing one time to find the length and then complementary or mutually redundant thinking.

Look for the penultimate K node public node Findnk (LinkedList list,int k) {if (k<=0) {System.out.println ("K should is upper than 0"); return null;} int n=0; Node P=list.getfirst (); while (p!=null) {n++;p =p.getnext ();} int Result=n-k;if (n-k<0) {System.out.println ("Index out of Range"); return null;} P=list.getfirst (); while (result!=0) {p=p.getnext (); result--;} return p;}
    • Question 3: Given a value x, the linked list is split into two parts, greater than the X part and the small and x parts.

When I received the question, I immediately popped an idea: sort. The order is solved. The fastest sorting algorithm is also O (N*LOGN). Then think of the quick sort, the quick sort of the partition function that splits the array at once into a sequence of dividing points with a datum element. Such a thought, the problem can be made O (n) complexity. The data structure required to implement it is a double-ended linked list.

Double-ended list split public void Dividelink (Doublelink dl,int Element) {Doublenode tail=dl.getend (). Getpre ();D Oublenode first= Dl.getfirst (); First.setdata (element), int start=1;int end=dl.getlength (), int x=element;while (start<end) {while ( Start<end && tail.getdata () >=x) {End--;tail=tail.getpre ();} if (start<end) {Start++;first.setdata (Tail.getdata ()); First=first.getnext ();} while (Start<end && first.getdata () <=x) {Start++;first=first.getnext ();} if (start<end) {Tail.setdata (First.getdata ()); End--;tail=tail.getpre ();}} First.setdata (x);}

  

Summary of linked list problems

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.