[Sword refers to Offer learning] [interview question 37: the first public node of two linked lists], sword refers to offer
Question: enter two linked lists to find their first public node.Linked List node Definition
/*** Linked list node class */private static class ListNode {int val; ListNode next; public ListNode () {} public ListNode (int val) {this. val = val ;}@ Override public String toString () {return val + "";}}
Solution:
First: Direct Method
Traverse each node in sequence on the first linked list. When traversing to a node, traverse each node in sequence on the second linked list. If there is a node on the second linked list and the node on the first linked list are the same, it means that the two linked lists overlap on this node, so they find their public nodes. If the length of the first linked list is m and the length of the second linked list is n, it is obvious that the time complexity of this method is O (mn ).
Method 2: Stack
So the two chains with public nodes and some of them are deprecated, And the topology looks like a Y, rather than X (5.3 ).
After analysis, we found that if two linked lists have public nodes, the public nodes appear at the end of the two linked lists. If we start from the end of two links, the last same node is the node we are looking.
In the above thinking, we need two yuan to help. If the chain table lengths are m and n, the space complexity is O (m + n ). The time complexity of this approach is O (m + n ). Compared with the initial brute force method, the time efficiency is improved, which is equivalent to the time efficiency in exchange for space consumption.
Third: First Method
In the two linked lists in Figure 5.3, We can traverse them to get their lengths of 5 and 4, that is, a longer linked list has more nodes than a shorter linked list. The second step is to go to the long linked list and reach Node 2. Next we traverse the two nodes from node 2 and node 4 at the same time until we find their first same node 6, which is what we want.
Compared with the second approach, the time complexity is O (m + brown), but we no longer need to facilitate the aggregation, thus improving the space efficiency.
This article uses the third solution
Code Implementation
Public class Test37 {/*** linked list node class */private static class ListNode {int val; ListNode next; public ListNode () {} public ListNode (int val) {this. val = val ;}@ Override public String toString () {return val + "" ;}}/*** find the first public node of the two nodes. If no public node is found, null is returned, the method is better. If there are null values in the two linked lists, ** @ param head1 first linked list * @ param head2 second linked list * @ return finds the public node, no null */public static ListNode findFirstCommonNode (ListNode h Ead1, ListNode head2) {int length1 = getListLength (head1); int length1 = getListLength (head2); int diff = length1-leng22. ListNode longListHead = head1; ListNode listen listhead = head2; if (diff <0) {longListHead = head2; inclulisthead = head1; diff = leng22-length1 ;}for (int I = 0; I <diff; I ++) {longListHead = longListHead. next;} while (longListHead! = Null & define listhead! = Null & longListHead! = Listhead) {longListHead = longListHead. next; initialize listhead = initialize listhead. next;} // return the first same public node. If no return null return longListHead;} private static int getListLength (ListNode head) {int result = 0; while (head! = Null) {result ++; head = head. next;} return result;} public static void main (String [] args) {test1 (); test2 (); test3 (); test4 ();} private static void test1 () {// The first public node is in the center of the linked list // 1-2-3 \ // 6-7 // 4-5/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); ListNode n7 = new ListNode (7); n1.next = n2; n2.next = n3; n3.next = n6; n6.next = n7; n4.next = n5; n5.next = n6; System. out. println (findFirstCommonNode (n1, n4); // 6} private static void test2 () {// No public node // 1-2-3-4 // 5-6-7 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); ListNode n7 = new ListNode (7); n1.next = n2; n2.next = n3; n3.next = n4; n5.next = n6; n6.next = n7; System. out. println (findFirstCommonNode (n1, n5); // null} private static void test3 () {// The Public node is the last node // 1-2-3-4 \ // 7 // 5-6/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); ListNode n7 = new ListNode (7); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n7; n5.next = n6; n6.next = n7; System. out. println (findFirstCommonNode (n1, n5); // 7} private static void test4 () {// The Public node is the first node // 1-2-3-4-5 // The two linked lists completely coincide with 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); ListNode n7 = new ListNode (7); n1.next = n2; n2.next = n3; n3.next = n4; n4.next = n5; System. out. println (findFirstCommonNode (n1, n1); // 1 }}
Running result
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.