First common node of two lists

Source: Internet
Author: User

Topic

Enter two linked lists to find their first common node.

Solving

Description
1. Single Linked list
2. The intersection will not be separated, because after the separation of a node has two next node is not a single linked list
Two linked lists presented Y Shape
If the two linked list two pointers go forward, the encounter node is the first common node, but the two pointers walk in different lengths
Two pointers walk a different length of Y Above the two forks, if the two pointer distribution should go two forks of the number of nodes, then the next node is their first public node.
If the length of the two linked list is calculated: alen,bLen
If a goes x,b and walks Y, meets
There is the following equation:
x + z = l e n
y +z =bLeN
z Represents the public length of two linked lists
Subtract from equation: x?y =aLeN?bLeN
The difference in the length of two lists is the difference in the number of nodes traversed by two pointers.
So, the long list, first go this difference
After that, two pointers go together
It was the first public junction after the meeting.

/*public class ListNode {int val;    ListNode next = null;    ListNode (int val) {this.val = val; }}*/ Public classSolution { PublicListNodeFindfirstcommonnode(ListNode pHead1, ListNode pHead2) {if(PHead1 = =NULL|| PHead2 = =NULL)return NULL;        ListNode P1 = pHead1; ListNode P2 = pHead2;intP1len = Depth (PHEAD1);intP2len = Depth (pHead2);if(P2len >p1len)returnFindfirstcommonnode (PHEAD2,PHEAD1);intdiff = P1len-p2len; while(diff!=0) {P1 = P1.next;        diff--; } while(p2len>0){if(P1 = = p2)returnP1;            P1 = P1.next;        P2 = p2.next; }returnP1; } Public int Depth(ListNode head) {if(Head = =NULL)return 0;if(Head.next = =NULL)return 1;return 1+ Depth (head.next); }}

See the hash in the discussion
Record each node that appears in the first list
Then traverse the second list to determine if the map already exists.

/*public class ListNode {int val;    ListNode next = null;    ListNode (int val) {this.val = val; }}*/Import Java.util.HashMap; Public classSolution { PublicListNodeFindfirstcommonnode(ListNode pHead1, ListNode pHead2)        {ListNode P1 = pHead1;        ListNode P2 = pHead2; Hashmap<listnode, integer> HashMap =NewHashmap<listnode, integer> (); while(P1! =NULL) {if(Hashmap.containskey (p1)) {hashmap.put (P1,hashmap).Get(p1) +1); }ElseHashmap.put (P1,1);        P1 = P1.next; } while(P2! =NULL) {if(Hashmap.containskey (p2))returnP2;        P2 = p2.next; }return NULL; }}

The following methods are not understood in the discussion

import java.util.HashMap;publicclass Solution {    publicFindFirstCommonNode(ListNode pHead1, ListNode pHead2) {        ListNode p1 = pHead1;        ListNode p2 = pHead2;        while(p1!=p2){            p1 = (p1==null ? pHead2 : p1.next);            p2 = (p2==null ? pHead1 : p2.next);        }        return p1;    }}

First common node of two lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.