Interview Algorithms Code Knowledge Carding Series
Algorithmic Knowledge Carding (1) - Sorting Algorithms
Algorithmic Knowledge Carding (2) - String Algorithms Part I
Algorithmic Knowledge Carding (3) - String Algorithms Part II
Algorithmic Knowledge Carding (4) - Array Part I
Algorithmic Knowledge Carding (5) - Array Part II
Algorithmic Knowledge Carding (6) - Array Part 3
Algorithmic Knowledge Carding (7) - Array Part IV
Algorithmic Knowledge Carding (8) - Binary Search Algorithms and Their Variations
I. outline
This paper introduces the Java code implementation of the linked list algorithm. All the code can be passed through Online Compiler Direct operation, algorithm directory:
- New linked list
- Inversion linked list (recursive and non-recursive implementation)
- Get the reciprocal k node of the list
- Get the middle node of the list
- Delete linked list nodes
- Switched Link List Node
In the discussion of this chapter, all linked lists contain a header node, Node, which does not store data. Its next pointer points to the first ordinary linked list node, and each ordinary linked list node contains an int-type data item.

2. Code Implementation
2.1 New Link List
Problem description
Enter an array of type int, create a linked list from the array, and print out all the elements of the linked list.
Solutions
Firstly, we create a header of the first node, and then we traverse the array p to get the elements and create the corresponding node, and do two steps:
- Take the current successor node of the first node as the new successor node of the new node
- A New Successive Node with a New Node as the First Node
Therefore, the order of nodes in the linked list is opposite to that of the original array.
code implementation
class Untitled { static class Node { public Node next; public int value; } static Node createList(int p[], int len) { Node header = new Node(); Node curNode = null; for (int i=0; i<len; i++) { curNode = new Node(); curNode.value = p[i]; //Take the old first common linked list node as the next of the new node. curNode.next = header.next; //Take the new node as the first common link list node. header.next = curNode; } return header; } static void printList(Node header) { if (header != null) { Node node = header.next; while (node != null) { System.out.println("value=" + node.value); node = node.next; } } } public static void main(String[] args) { int p[] = {1,2,3,4,5}; Node header = createList(p, p.length); printList(header); } }
Operation result
>> value=5 >> value=4 >> value=3 >> value=2 >> value=1
2.2 Inversion Link List
Problem description
Inverse the input list. For example, the list created in 2.1 is header - > 5 - > 4 - > 3 - > 2 - > 1, and the list after inversion is header - > 1 - > 2 - > 3 - > 4 - > 5.
Solutions
Here we introduce two ways: non-recursive implementation and recursive implementation.
Implementation code
class Untitled { static class Node { public Node next; public int value; } static Node createList(int p[], int len) { Node header = new Node(); Node curNode = null; for (int i=0; i<len; i++) { curNode = new Node(); curNode.value = p[i]; //Take the old first common linked list node as the next of the new node. curNode.next = header.next; //Take the new node as the first common link list node. header.next = curNode; } return header; } //(1) Non-recursive implementation static void reverseList(Node header) { if (header == null) { return; } //curNode denotes the node to be inverted. Node curNode = header.next; //nextNode represents the next node of the node to be inverted. Node nextNode = null; //curHeader represents the first common node in the list section that has completed the inversion. Node curHeader = null; while (curNode != null) { nextNode = curNode.next; curNode.next = curHeader; curHeader = curNode; curNode = nextNode; } header.next = curHeader; } //(2) Recursive implementation static void reverseListDi(Node header) { if (header == null) { return; } reverseListDiCore(header.next, header); } static Node reverseListDiCore(Node header, Node listHeader) { if (header.next == null) { listHeader.next = header; return header; } //Next node. Node nextNode = header.next; //Reverse the next node. Node reverseNode = reverseListDiCore(nextNode, listHeader); //Re-establish the relationship between the current node and the next node. reverseNode.next = header; header.next = null; return header; } static void printList(Node header) { if (header != null) { Node node = header.next; while (node != null) { System.out.println("value=" + node.value); node = node.next; } } } public static void main(String[] args) { int p[] = {1,2,3,4,5}; Node header = createList(p, p.length); reverseListDi(header); printList(header); } }
Operation result
>> value=1 >> value=2 >> value=3 >> value=4 >> value=5
2.3 Get the reciprocal k node of the list
Problem description
Enter a linked list and return the k-th imported node of the linked list (excluding the first node, and the last node is the penultimate node). If the length of the linked list is less than k, return null.
Solutions
Using the idea of fast and slow pointer, let fast take k steps first, then slow pointer begins to walk with fast pointer. When fast is at the last node, then slow is at the last k node.
code implementation
class Untitled { static class Node { public Node next; public int value; } static Node createList(int p[], int len) { Node header = new Node(); Node curNode = null; for (int i=0; i<len; i++) { curNode = new Node(); curNode.value = p[i]; //Take the old first common linked list node as the next of the new node. curNode.next = header.next; //Take the new node as the first common link list node. header.next = curNode; } return header; } static Node getLastKNode(Node header, int k) { if (k < 1) { return null; } Node fast = header; Node slow = header; int step = 0; while (fast != null && fast.next != null) { fast = fast.next; step++; if (step >= k) { slow = slow.next; } } return slow != header ? slow : null; } static void printList(Node header) { if (header != null) { Node node = header.next; StringBuilder builder = new StringBuilder(); while (node != null) { builder.append(String.valueOf(node.value)); node = node.next; if (node != null) { builder.append("->"); } } System.out.println(builder.toString()); } } public static void main(String[] args) { int p[] = {1,2,3,4,5}; Node header = createList(p, p.length); printList(header); Node kNode = getLastKNode(header, 4); System.out.println("KNode=" + (kNode != null ? kNode.value : "")); } }
Operation result
>> 5->4->3->2->1 >> KNode=4
2.4 Get the middle node of the list
Problem description
Enter a list to get the middle node of the list:
- If the length of the list is 1, the only node is returned.
- If the length of the list is even, the return node is its len/2 node, where len is the length of the list.
- If the length of the list is odd, then the value of len/2 is x.5 and the return node is x.5+0.5.
Solutions
Similar to 2.3, fast takes two steps at a time, and slow takes one step at a time. When fast traverses to the end, slow is in the middle.
Implementation code
class Untitled { static class Node { public Node next; public int value; } static Node createList(int p[], int len) { Node header = new Node(); Node curNode = null; for (int i=0; i<len; i++) { curNode = new Node(); curNode.value = p[i]; //Take the old first common linked list node as the next of the new node. curNode.next = header.next; //Take the new node as the first common link list node. header.next = curNode; } return header; } static Node geMiddleNode(Node header) { if (header == null || header.next == null) { return null; } Node fast = header; Node slow = header; while (fast != null) { fast = fast.next; if (fast != null) { fast = fast.next; } else { break; } slow = slow.next; } return slow; } static void printList(Node header) { if (header != null) { Node node = header.next; StringBuilder builder = new StringBuilder(); while (node != null) { builder.append(String.valueOf(node.value)); node = node.next; if (node != null) { builder.append("->"); } } System.out.println(builder.toString()); } } public static void main(String[] args) { int p[] = {1,2,3}; Node header = createList(p, p.length); printList(header); Node kNode = geMiddleNode(header); System.out.println("KNode=" + (kNode != null ? kNode.value : "")); } }
2.5 Delete linked list nodes
Problem description
Enter the head node header of a linked list and give a node dNode located in the linked list. It is required to delete the node from the linked list.
Solutions
The easiest way to think about this problem is to find the precursor node and the successor node of the node to be deleted, and let the next of the precursor node point to the successor node to achieve deletion. But for the case that the node to be deleted is not the end node, we can adopt a small skill: take out the successor node of the node to be deleted, and delete the successor node, so as to avoid looking for the precursor node. The process.
Implementation code
class Untitled { static class Node { public Node next; public int value; } static Node createList(int p[], int len) { Node header = new Node(); Node curNode = null; for (int i=0; i<len; i++) { curNode = new Node(); curNode.value = p[i]; //Take the old first common linked list node as the next of the new node. curNode.next = header.next; //Take the new node as the first common link list node. header.next = curNode; } return header; } static Node getLastKNode(Node header, int k) { if (k < 1) { return null; } Node fast = header; Node slow = header; int step = 0; while (fast != null && fast.next != null) { fast = fast.next; step++; if (step >= k) { slow = slow.next; } } return slow != header ? slow : null; } static void deleteNode(Node header, Node dNode) { if (header == null && dNode != null) { return; } if (dNode.next != null) { //If it is not the endpoint, then replace the node to be deleted with the value of the subsequent node. Node rNode = dNode.next; dNode.value = rNode.value; dNode.next = rNode.next; } else { //If it is the end point, then it can only be traversed. Node node = header; while (node.next != null && node.next.next != null) { node = node.next; } node.next = null; } } static void printList(Node header) { if (header != null) { Node node = header.next; StringBuilder builder = new StringBuilder(); while (node != null) { builder.append(String.valueOf(node.value)); node = node.next; if (node != null) { builder.append("->"); } } System.out.println(builder.toString()); } } public static void main(String[] args) { int p[] = {1,2,3}; Node header = createList(p, p.length); printList(header); Node kNode = getLastKNode(header, 3); System.out.println("KNode=" + (kNode != null ? kNode.value : "")); deleteNode(header, kNode); printList(header); } }
Operation result
>> 3->2->1 >> KNode=2 >> 3->1
2.6 Switched Link List Nodes
Problem description
Given a header of a single linked list, and two linked list nodes nodeA and nodeB to be exchanged, exchange the two linked list nodes
Solutions
The key of interactive linked list nodes is to find the precursor and successor nodes of these two nodes, and modify their reference relationship with the corresponding nodes. What we need to pay attention to here is the situation that the exchange nodes are adjacent to each other.
code implementation
class Untitled { static class Node { public Node next; public int value; } static Node createList(int p[], int len) { Node header = new Node(); Node curNode = null; for (int i=0; i<len; i++) { curNode = new Node(); curNode.value = p[i]; //Take the old first common linked list node as the next of the new node. curNode.next = header.next; //Take the new node as the first common link list node. header.next = curNode; } return header; } static Node getLastKNode(Node header, int k) { if (k < 1) { return null; } Node fast = header; Node slow = header; int step = 0; while (fast != null && fast.next != null) { fast = fast.next; step++; if (step >= k) { slow = slow.next; } } return slow != header ? slow : null; } static void swapNode(Node header, Node nodeA, Node nodeB) { if (header == null || nodeA == null || nodeB == null) { return; } if (nodeA == header || nodeB == header) { return; } if (nodeA == nodeB) { return; } //Find the precursor node of nodeA Node preA = header; while (preA.next != nodeA) { preA = preA.next; } //Find the precursor node of nodeB Node preB = header; while (preB.next != nodeB) { preB = preB.next; } //Subsequent Nodes of NoeA and NoeB Node postA = nodeA.next; Node postB = nodeB.next; //NoeA is the successor node of NoeB if (preB == nodeA) { nodeA.next = postB; nodeB.next = nodeA; preA.next = nodeB; //NoeB is the successor node of NoeA } else if (preA == nodeB) { nodeB.next = postA; nodeA.next = nodeB; preB.next = nodeA; //NoeA and NoeB are not adjacent } else { preA.next = nodeB; nodeB.next = postA; preB.next = nodeA; nodeA.next = postB; } } static void printList(Node header) { if (header != null) { Node node = header.next; StringBuilder builder = new StringBuilder(); while (node != null) { builder.append(String.valueOf(node.value)); node = node.next; if (node != null) { builder.append("->"); } } System.out.println(builder.toString()); } } public static void main(String[] args) { int p[] = {1,2,3,4,5}; Node header = createList(p, p.length); printList(header); Node nodeA = getLastKNode(header, 5); Node nodeB = getLastKNode(header, 1); swapNode(header, nodeA, nodeB); printList(header); } }
Operation result
>> 5->4->3->2->1 >> 1->4->3->2->5
For more articles, please visit my Android Knowledge Carding Series:
- Android Knowledge Catalog: http://www.jianshu.com/p/fd82d18994ce
- Android Interview Document Sharing: http://www.jianshu.com/p/8456fe6b27c4