Definition of linked list:
public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }
-
Enter a linked list and return an ArrayList in the end-to-end order of the linked list values.
This topic is implemented by recursive call. It can also be accomplished by using the feature of stack first in and then out. It can also be directly inverted by Collections.reverse(list) method, which is not discussed here.
import java.util.ArrayList; public class Solution { ArrayList<Integer> array = new ArrayList<Integer>(); public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { if (listNode != null) { this.printListFromTailToHead(listNode.next); array.add(listNode.val); } return array; } }
-
Input a list and output the reciprocal k node in the list.
This paper uses two pointers to realize a measuring ruler of length k. When the first pointer reaches the end of the list, the second pointer is located at the reciprocal k node.
public class Solution { public ListNode FindKthToTail(ListNode head,int k) { ListNode h = head; ListNode t = head; int i = 0; for (; h != null; i++) { if (i >= k) { t = t.next; } h = h.next; } return i >= k ? t : null; } }
Note: When the total number of nodes in the list is less than k, null is returned.
-
Input a list, invert the list, and output the header of the new list.
Ontology uses two pointers pre to point to the previous node of the current node and next to point to the next node of the current node to realize the inversion of the list.
Method A Non-recursive Method
The implementation steps are as follows:
Step 1, head next1 next2
Step 2, next = head.next; implement next next1 next2, which is used to save the nodes after the head
Step 3, head.next = pre; implement head pre, complete the inversion of the list
Step 4. Finally, head and pre move a node backwards to repeat the process until head = null.public class Solution { public ListNode ReverseList(ListNode head) { ListNode pre = null; ListNode next = null; if (head == null) { return null; } while (head != null) { next = head.next; head.next = pre; pre = head; head = next; } return pre; } }
Method Two Recursive Method
First, we use recursion to reach the last node, assign it to pre, which remains unchanged, and then execute it.
head.next.next = head head.next = null;
In these two sentences, the head is the penultimate node, and the head.next.next = head implementation points the penultimate node to the penultimate node, and then the second node to the empty. The next recursive implementation points the penultimate node to the penultimate three nodes until the end of the recursion. In this process, the Preis always the last node of the original list, and finally returns to the Preto realize the inversion of the entire list.
Question: Why did the list not break in this process?
In the last recursion, the next = null of the former node, why the node in the next recursion can make the last node point to the node in this recursion is not understood.public class Solution { public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode pre = ReverseList(head.next); head.next.next = head; head.next = null; return pre; } }
-
Input two monotonically increasing linked lists, output two combined linked lists, of course, we need to synthesize the linked list to meet the monotonic rule.
public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { if (list1 == null) { return list2; } if (list2 == null) { return list1; } ListNode mergeHead = null; ListNode current = null; while ((list1 != null) && (list2 != null)) { if (list1.val <= list2.val) { if (mergeHead == null) { mergeHead = list1; current = list1; } else { current.next = list1; current = current.next; } list1 = list1.next; } else { if (mergeHead == null) { mergeHead = list2; current = list2; } else { current.next = list2; current = current.next; } list2 = list2.next; } } if (list1 == null) { current.next = list2; } if (list2 == null) { current.next = list1; } return mergeHead; } }