[Sword refers to Offer learning] [interview question 56: entry point in the central link of the linked list], offer central
Question: How can I find the entry point of a linked list containing a ring?Solutions
You can use two pointers to solve this problem. First, define two pointers P1 and P2 pointing to the head node of the linked list. If there are n nodes in the central part of the linked list, the pointer P1 moves n steps forward on the linked list, and then the two pointers move forward at the same speed. When the second Pointer Points to the entry point of the ring, the first pointer has walked around the ring and returned to the entry point.
The remaining question is how to get the number of nodes in the ring. We used two pointers, one fast and one slow, for the second related question in question 15. If the two pointers meet each other, the linked list contains loops. The nodes where the two pointers meet must be in the ring. You can start from this node and continue to move forward and count. When you return to this node again, you can get the number of knots in the ring.
Node Definition
private static class ListNode { private int val; private ListNode next; public ListNode() { } public ListNode(int val) { this.val = val; } @Override public String toString() { return val +""; } }
Code Implementation
Public class Test56 {private static class ListNode {private int val; private ListNode next; public ListNode () {} public ListNode (int val) {this. val = val ;}@ Override public String toString () {return val + "" ;}} public static ListNode meetingNode (ListNode head) {ListNode fast = head; ListNode slow = head; while (fast! = Null & fast. next! = Null) {fast = fast. next. next; slow = slow. next; if (fast = slow) {break ;}// the chain table does not contain the if (fast = null | fast. next = null) {return null;} // fast points to the first node fast = head again; while (fast! = Slow) {fast = fast. next; slow = slow. next;} return fast;} public static void main (String [] args) {test01 (); test02 (); test03 ();} // 1-> 2-> 3-> 4-> 5-> 6 private static void test01 () {ListNode n1 = new ListNode (1 ); listNode n2 = new ListNode (2); ListNode n3 = new ListNode (3); ListNode n4 = new ListNode (4); ListNode n5 = new ListNode (5 ); listNode n6 = new ListNode (6); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; System. out. println (meetingNode (n1 ));} // 1-> 2-> 3-> 4-> 5-> 6 // ^ | // + -------- + private static void test02 () {ListNode n1 = new ListNode (1); ListNode n2 = new ListNode (2); ListNode n3 = new ListNode (3); ListNode n4 = new ListNode (4 ); listNode n5 = new ListNode (5); ListNode n6 = new ListNode (6); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; n6.next = n3; System. out. println (meetingNode (n1 ));} // 1-> 2-> 3-> 4-> 5-> 6 <-+/| // + --- + private static void test03 () {ListNode n1 = new ListNode (1); ListNode n2 = new ListNode (2); ListNode n3 = new ListNode (3); ListNode n4 = new ListNode (4 ); listNode n5 = new ListNode (5); ListNode n6 = new ListNode (6); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; n5.next = n6; n6.next = n6; System. out. println (meetingNode (n1 ));}}
Running result
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.