Summarize and improve, with June encouragement
Overview
Data structure and algorithm of the eternal theme, linked list is also the question of interview frequently, especially the handwriting code often appear, will be a summary from the following aspects
"Number of linked lists"
"Reverse Linked list-loop"
"Reverse linked List-recursion"
"Find the bottom of the list K nodes"
"Find linked list intermediate nodes"
"Judging if the chain list has a ring"
"Print a single linked list from the end of the tail-recursion"
"Print single linked list-stack" from tail to head
"From small to large merge ordered single linked list-loop"
"From small to large merge ordered single linked list-recursion"
It is common to define a single-linked list structure in Java
<span style= "Font-family:microsoft yahei;font-size:14px;" >class Node{int value; Node next;public node (int n) {this.value = N;this.next = null;}} </span>
1. Number of linked lists
This is relatively simple, not to repeat
<span style= "Font-family:microsoft yahei;font-size:14px;" >//the number of nodes in a single linked list public int Getlistlen (node head) {int len = 0;while (head! = null) {len++;head = Head.next;} return Len;} </span>
2. Reverse linked list-cycle
Using a double pointer, mainly 4 lines of code, of which 2, 3 lines to complete the pointer reversal, 1, 4 is mainly to keep head downward pointing
<span style= "Font-family:microsoft yahei;font-size:14px;" >//reverse single-linked list (loop) public Node reverselist (node head) {//Security check if (head = = NULL | | head.next = = NULL) return head; Node pre = NULL; Node temp = Null;while (head! = NULL) {//The following 1234 refers to the following four lines of code temp = head.next;//with the 4th line corresponding to complete the head node move head.next = pre;//and 3rd line corresponding to complete the reverse pre = head;//corresponds to the 2nd line to complete the reverse head = temp;//and 1th lines corresponding to the completion of the head node move}return pre; </span>
3. Reverse linked List-recursion
<span style= "Font-family:microsoft yahei;font-size:14px;" >//reverses the single-linked list, recursively public static node Reverselistrec (node head) {if (head = = NULL | | head.next = = NULL) return head; Node Rehead = Reverselistrec (head.next); head.next.next = Head;head.next = Null;return rehead;} </span>
4. Find the countdown k node of the list
Double pointer method, not much explanation
<span style= "Font-family:microsoft yahei;font-size:14px;" >//find a single-linked list the reciprocal K-node double-pointer method public Node reknode (node head, int k) {if (head = = null) return Head;int len = Getlistlen (head); if (k > Len) return null; Node TARGETK = head; Node NEXTK = head;//First go to K position for (int i = 0; i < K; i++) {nextk = Nextk.next;} Go with the head node again, NEXTK to the end, at which point the TARGETK is the penultimate K node while (NEXTK! = null) {NEXTK = NEXTK.NEXT;TARGETK = Targetk.next;} return TARGETK;} </span>
5. Find the middle node of the linked list
Fast and slow hands, not much explanation
<span style= "Font-family:microsoft yahei;font-size:14px;" >public node Getmid (node Head) {//similar fast/slow pointer method//Security check if (head = = NULL | | head.next = = NULL) return head; Node target = head; Node temp = head;while (temp! = NULL && Temp.next! = null) {target = Target.next;temp = Temp.next.next;} return target;} </span>
6. Determine if the chain list has a ring
The main or slow pointer, if the fast pointer can catch the slow pointer is a ring
<span style= "Font-family:microsoft yahei;font-size:14px;" >//determine if there is a ring in a single-linked list, the fast pointer public boolean hascycle (Node head) {Boolean flag = false; Node P1 = head; Node P2 = head;while (P1! = null && P2! = null) {P1 = P1.next;p2 = p2.next.next;if (P2 = = p1) {flag = True;break;} }return Flag;} </span>
7. Print single linked list from tail to end-recursion
<span style= "Font-family:microsoft yahei;font-size:14px;" >//print single-linked list (recursive) public void ReList1 (Node head) {//Security check if (head = = null) return;else {reList1 (head.next) from tail to head System.out.println (Head.value);}} </span>
8. Print a single linked list from the end of the head-stack
Using the properties of the stack filo, first store the node and then output the node value of each stack.
<span style= "Font-family:microsoft yahei;font-size:14px;" >//print single-linked list (stack) public void ReList2 (Node head) {stack<node> s = new stack<node> (), while (head! = null) {S.P Ush (head); head = Head.next;} while (!s.isempty ()) {System.out.println (S.pop (). value);}} </span>
9, from small to large combined ordered single chain list-cycle
<span style= "Font-family:microsoft yahei;font-size:14px;" >//from small to large merge two ordered single-linked lists (loops) public Node mergeSort1 (node head1, node head2) {//Security check if (Head1 = null) return head2;if (head2 = = null) return head1;//new merge node, node target = null;//Determine the first element's nodes if (Head1.value > Head2.value) {target = Head2;head2 = He Ad2.next;} else {target = Head1;head1 = Head1.next;} Target.next = null;//begins merging node Mergehead = Target;while (head1! = NULL && head2! = null) {//When two linked lists are not empty if (Head1.va Lue > Head2.value) {target.next = Head2;head2 = Head2.next;} else {target.next = Head1;head1 = Head1.next;} target = Target.next;target.next = null;} if (head1 = = null) Target.next = Head2;elsetarget.next = Head1;return mergehead;} </span>
10, from small to large combined ordered single linked list-recursion
<span style= "Font-family:microsoft yahei;font-size:14px;" >//from small to large merge two ordered single-linked lists (recursive) public Node mergeSort2 (node head1, node head2) {if (head1 = null) return head2;if (head2 = null) Return head1;if (Head1.value > Head2.value) {head2.next = MergeSort2 (Head2.next, head1); return head2;} else {Head1.nex t = MergeSort2 (Head1.next, head2); return head1;}} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java single-linked list common operations