"title" Enter two linked lists to find their first common node.
* "idea" 1 gets the length of the two linked lists;
* 2 Let the long list go first n steps and then go to the start position of the short list;
* 32 lists are traversed at the same time, until the same, returning to the first common node.
1 PackageCom.exe11.offer;2 3 /**4 * "title" Enter two linked lists to find their first common node. 5 * "idea" 1 gets the length of the two linked lists;6 * 2 Let the long list go first n steps and then go to the start position of the short list;7 * 32 lists are traversed at the same time, until the same, returning to the first common node. 8 * @authorWGS9 *Ten */ One Public classCommonlistnode { A - Static classlistnode{ - intVal; theListNode next=NULL; - PublicListNode (intval) { - This. val=Val; - } + } - PublicListNode getcommonnode (listnode phead1,listnode pHead2) { + if(phead1==NULL|| phead2==NULL) A return NULL; at //1 get two linked list lengths - intlen1=getlistnodelength (pHead1); - intLen2=getlistnodelength (pHead2); - //2 Comparison length - intDifval=len1-Len2; -ListNode longlistnode=PHead1; inListNode shortlistnode=phead2;//Suppose PHead1 long, pHead2 short - if(difval<0) {//Len1<len2,phead2 Long toLonglistnode=pHead2; +Shortlistnode=PHead1; -difval=len2-len1; the } * //3 Let the long go First n (difval) step $ for(inti=0;i<difval;i++){Panax NotoginsengLonglistnode=Longlistnode.next; - } the //4 Go together again. + while(longlistnode!=NULL&& shortlistnode!=NULL&& (longlistnode.val!=shortlistnode.val)) { ALonglistnode=Longlistnode.next; theShortlistnode=Shortlistnode.next; + } - //when the same value is the common node, jump out of the loop . $ListNode commonnode=Longlistnode; $ /*if (longlistnode.val==shortlistnode.val) { - commonnode=shortlistnode;//return to the public node. - }*/ the - returnCommonnode;Wuyi the } - //Get the chain table length Wu Private intgetlistnodelength (ListNode phead) { -ListNode node=Phead; About intLen=0; $ while(node!=NULL){ -len++; -Node=Node.next; - } A returnLen; + } the - $ the the the the Public Static voidMain (string[] args) { -ListNode PHead1 =NewListNode (1); inListNode Node1 =NewListNode (2); theListNode Node2 =NewListNode (3); theListNode Node3 =NewListNode (4); About //ListNode node4 = new ListNode (7); thePhead1.next =Node1; theNode1.next =Node2; theNode2.next =Node3; + //node3.next = node4; - theListNode pHead2 =NewListNode (5);BayiListNode NODE5 =NewListNode (6); theListNode Node6 =NewListNode (7); the //ListNode node7 = new ListNode (7); -Phead2.next =Node5; -Node5.next =Node6; the //node6.next = Node7; the theCommonlistnode c=NewCommonlistnode (); theListNode node=C.getcommonnode (PHead1, pHead2); - System.out.println (node); the the } the 94}
Alternatively, use a collection to first add all the values of the first linked list, followed by the second linked list value, when the first one is the same as the first public node.
1 PackageCom.exe11.offer;2 3 ImportJava.util.HashMap;4 5 /**6 * "title" Enter two linked lists to find their first common node. 7 * "idea" 1 gets the length of the two linked lists;8 * 2 Let the long list go first n steps and then go to the start position of the short list;9 * 32 lists are traversed at the same time, until the same, returning to the first common node. Ten * @authorWGS One * A */ - Public classCommonListNode2 { - the Static classlistnode{ - intVal; -ListNode next=NULL; - PublicListNode (intval) { + This. val=Val; - } + } A PublicListNode getcommonnode (listnode phead1,listnode pHead2) { atHashmap<listnode,integer> map=NewHashmap<>(); - //Add all the PHead1 values to the collection first - while(phead1!=NULL){ -Map.put (PHead1, 1); -phead1=Phead1.next; - } in // - //ListNode commonnode=null; to while(phead2!=NULL){ +Integer node=Map.get (pHead2); - if(node!=NULL) the returnpHead2; *Phead2=Phead2.next; $ }Panax Notoginseng return NULL; - the } + A the + - Public Static voidMain (string[] args) { $ListNode PHead1 =NewListNode (1); $ListNode Node1 =NewListNode (2); -ListNode Node2 =NewListNode (3); -ListNode Node3 =NewListNode (4); the //ListNode node4 = new ListNode (7); -Phead1.next =Node1;WuyiNode1.next =Node2; theNode2.next =Node3; - //node3.next = node4; Wu -ListNode pHead2 =NewListNode (5); AboutListNode NODE5 =NewListNode (6); $ListNode Node6 =NewListNode (7); - //ListNode node7 = new ListNode (7); -Phead2.next =Node5; -Node5.next =Node6; A //node6.next = Node7; + theCommonListNode2 c=NewCommonListNode2 (); -ListNode node=C.getcommonnode (PHead1, pHead2); $ System.out.println (node.val); the the } the the}
Sword means offer series---two lists of common nodes