Java linked list common question summary, java question Summary
First, define the custom node class to store the node information:
public class Node { Node next=null; int data; public Node(int data){ this.data=data; }}
Get the chain table length:
private int length() { int length=0; Node temp=head; while(temp!=null){ length++; temp=temp.next; } return length; }
Print linked list:
public void printMyList(){ Node temp=this.head; while(temp!=null){ System.out.print(temp.data); temp=temp.next; if(temp!=null){ System.out.print("-->"); }else{ System.out.println(); } } }
Insert data into the linked list:
public void addNode(int d){ Node node=new Node(d); if(head==null){ head=node; return; } Node temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=node; }
Insert a node to the linked list:
public void addNode(Node node){ if(head==null){ head=node; return; } Node temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=node; }
Add another linked list at the end of the linked list:
public static void addList(MyLinkedList list,MyLinkedList afterlist) { Node thishead = list.head; Node addhead = afterlist.head; if(thishead==null){ thishead=addhead; return; } Node temp=thishead; while(temp.next!=null){ temp=temp.next; } temp.next=addhead; }
Delete data at the specified position from the linked list:
Public boolean deleteNode (int index) {// index: if (index <1 | index> length () {return false ;} if (index = 1) {head = head. next; return true;} int I = 2; Node preNode = head; Node curNode = preNode. next; while (curNode! = Null) {if (I = index) {preNode. next = curNode. next; return true;} preNode = curNode; curNode = curNode. next; I ++;} return true ;}
Sort the linked list and return the sorted header node:
public Node orderList(){ Node nextNode=null; int temp=0; Node headNode=head; while(headNode.next!=null){ nextNode=headNode.next; while(nextNode!=null){ if(headNode.data>nextNode.data){ temp=nextNode.data; nextNode.data=headNode.data; headNode.data=temp; } nextNode=nextNode.next; } headNode=headNode.next; } return head; }
The first method to delete duplicate data from the linked list
public void deleteRepetition1(){ Hashtable<Integer, Integer>hashtable=new Hashtable<>(); Node temp=this.head; Node pre=null; while(temp!=null){ if(hashtable.containsKey(temp.data)) { pre.next=temp.next; } else { hashtable.put(temp.data, 1); pre=temp; } temp=temp.next; } }
The second method is to delete duplicate data from the linked list:
public void deleteRepetition2(){ Node temp=head; while(temp!=null){ Node i=temp; while(i.next!=null){ if(temp.data==i.next.data) { i.next=i.next.next; } else { i=i.next; } } temp=temp.next; } }
Find the last k elements in a single-chain table:
Public Node findLastElem (int k) {if (k <1) {System. out. println ("k illegal"); return null;} if (head = null) {System. out. println ("linked list does not contain elements"); return null;} Node p1 = head; Node p2 = head; for (int I = 0; I <K-1; I ++) {p2 = p2.next;} while (p2.next! = Null) {p1 = p1.next; p2 = p2.next;} return p1 ;}
Chain table reversal:
public void reversal(){ Node pReversalHead=this.head; Node pNode=this.head; Node pPrev=null; while(pNode!=null){ Node pNext=pNode.next; if(pNext==null) { pReversalHead=pNode; } pNode.next=pPrev; pPrev=pNode; pNode=pNext; } this.head=pReversalHead; }
If the linked list is not reversed, elements of the linked list are output in reverse order:
public void printReversalList(Node pHead){ if(pHead.next==null){ System.out.print(pHead.data); } if(pHead.next!=null){ printReversalList(pHead.next); System.out.print("-->"+pHead.data); } }
Find the intermediate node of a single-chain table:
public Node searchMidNode(){ Node pNode=this.head; Node qNode=this.head; while(pNode!=null&&pNode.next!=null&&pNode.next.next!=null){ pNode=pNode.next.next; qNode=qNode.next; } return qNode; }
Determine whether a linked list has a ring:
public boolean isHaveLoop(){ Node fast=head; Node slow=head; if(fast==null){ return false; } while(fast!=null&&fast.next!=null){ fast=fast.next.next; slow=slow.next; if(fast==slow){ return true; } } return false; }
Search for the Ring entry:
Public Node getLoopStart (mydomainlist list) {// you can set a pointer at the head and intersection of the linked list. Each time you take a step, the two pointers must meet each other, and the first encounter point is the ring entry Node slow = list. head; Node fast = list. head; while (slow. next! = Null & fast. next! = Null) {slow = slow. next; fast = fast. next. next; if (slow = fast) {break ;}} while (slow! = Fast) {slow = slow. next; fast = fast. next;} return slow ;}
Delete a specified node without knowing the header pointer:
Public boolean deleteNode (Node node) {// This method cannot delete the last node of the linked list, because it cannot direct the next node of its precursor Node to null; if (node = null | node. next = null) {return false;} node. data = node. next. data; node. next = node. next. next; return true ;}
Determine whether two linked lists are intersecting:
public static boolean isIntersect(MyLinkedList list,MyLinkedList list2){ Node head1=list.head; Node head2=list2.head; if(head1==null||head2==null){ return false; } while(head1.next!=null){ head1=head1.next; } while(head2.next!=null){ head2=head2.next; } return head1==head2; }
Find the first node of the intersection of two linked lists:
public static Node getFirstMeetNode(MyLinkedList list,MyLinkedList list2){ Node head1=list.head; Node head2=list2.head; if (isIntersect(list, list2)==false){ return null; } Node t1=head1; Node t2=head2; if(list.length()>list2.length()){ int d=list.length()-list2.length(); while(d!=0){ t1=t1.next; d--; } } else { int d=list2.length()-list.length(); while(d!=0){ t2=t2.next; d--; } } while(t1!=t2){ t1=t1.next; t2=t2.next; } return t1; }