Find out the first public node of a linked list

Source: Internet
Author: User

62. Find out the first common node of the list.
Title: Two one-way lists to find their first common node

Ideas:

1. Violence law but time complexity is too high O (n*m)

2. If the two linked list has a public node, then from the public node, the subsequent nodes are public, that is, the two linked lists intersect at the first common node, and then form a Y-shape, because two of the list length is not necessarily the same, so you can first obtain their length n and M, and then get the length difference: math.abs ( N-M), and then first traverse the long list of N-M nodes, starting from the N-m+1 node to traverse the two linked lists, and then find the first one is the same (online copy of others)

3. The bitmap method for two linked lists to establish bitmap, and then to the two bitmap to perform and operation, then the result of the leftmost 1 represents the first identical element, and then to the value of each right one bit, until equal to 1, the number of times to move to the right is the first common element, the time complexity of O (n +M)

1  PackageCom.rui.microsoft;2 3 //62. Find out the first common node of the list. 4 //title: Two one-way lists to find their first common node5  Public classtest62_findcommonnodefromlinkedlists {6 7      Public Static voidMain (string[] args) {8Node Node1 =NewNode (1);9Node Node2 =NewNode (2);TenNode Node3 =NewNode (3); OneNode1.next =Node2; ANode2.next =Node3; -          -Node node4 =NewNode (4); theNode NODE5 =NewNode (5); -Node NodeS =NewNode (2); -Node Node6 =NewNode (1); -Node4.next =Node5; +Node5.next =NodeS; -Nodes.next =Node6; +          ATest62_findcommonnodefromlinkedlists app =Newtest62_findcommonnodefromlinkedlists (); at App.find (Node1, node4); -     } -      -     //bit Operation Method -     //time Complexity O (n+m) = Traverse List construction bitmap -     voidFind (node HEADX, node heady) { inNode x =HEADX; -Node y =Heady; to          +         intBITX = 0; -         intbity = 0; the          *          while(NULL!=x) { $BITX |= 1 <<X.value;Panax Notoginsengx =X.next; -         } the          +          while(NULL!=y) { ABity |= 1 <<Y.value; they =Y.next; +         } -          $ System.out.println (integer.tobinarystring (BITX)); $ System.out.println (integer.tobinarystring (bity)); -          -         intInter = BITX &bity; theString Interstr =integer.tobinarystring (inter); - System.out.println (INTERSTR);Wuyi          the         intRadix = 0; -          while(Inter > 1){ WuInter >>= 1; -radix++; About         } $ System.out.println (radix); -     } -      -     Static classNode { A         intvalue; + Node Next; the          PublicNode (intv) { -              This. Value =v; $         } the     } the}

Find out the first public node of a linked list

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.