You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionintersectNode(head1,head2){vararr1=[],arr2=[],theValue=null;transformToArray(head1,arr1);transformToArray(head2,arr2);for(vari=0;i<arr1.length;i++){if(arr2.indexOf(arr1[i])!==-1){theValue=arr1[i];return'Reference of the node with value = '+theValue;}}returntheValue;/* arr1.some( value => { if ( arr2.includes(value) ){ theValue = value; return true; } }) if( theValue ){ return 'Reference of the node with value = ' + theValue; } return null; */}functiontransformToArray(head,arr){while(head){arr.push(head.val);head=head.next;}}
测试代码:
functionCreateNode(val){this.val=val;this.next=null;}functionCreateList(...nodes){this.head=nodes[0];this.length=nodes.length;for(vari=0;i<nodes.length-1;i++){if(nodes[i+1]){nodes[i].next=nodes[i+1];}}}functionintersectNode(head1,head2){vararr1=[],arr2=[],theValue=null;transformToArray(head1,arr1);transformToArray(head2,arr2);for(vari=0;i<arr1.length;i++){if(arr2.indexOf(arr1[i])!==-1){theValue=arr1[i];return'Reference of the node with value = '+theValue;}}returntheValue;}functiontransformToArray(head,arr){while(head){arr.push(head.val);head=head.next;}}constnode1=newCreateNode(1);constnode2=newCreateNode(2);constnode3=newCreateNode(3);constnode4=newCreateNode(4);constnode5=newCreateNode(5);constnodeA=newCreateNode('A');constnodeB=newCreateNode('B');constnodeC=newCreateNode('C');constnodeD=newCreateNode('D');constnodeE=newCreateNode('E');constlist1=newCreateList(node1,node2,node3,node4,node5);constlist2=newCreateList(nodeA,nodeB,node3,nodeC,nodeD);intersectNode(list1.head,list2.head);
functionintersectNode(head1,head2){vararr1=[],arr2=[],theValue=null;transformToArray(head1,arr1);transformToArray(head2,arr2);for(vari=0;i<arr1.length;i++){if(arr2.indexOf(arr1[i])!==-1){theValue=arr1[i];return'Reference of the node with value = '+theValue;}}returntheValue;/* arr1.some( value => { if ( arr2.includes(value) ){ theValue = value; return true; } }) if( theValue ){ return 'Reference of the node with value = ' + theValue; } return null; */}functiontransformToArray(head,arr){while(head){arr.push(head.val);head=head.next;}}
测试代码:
functionCreateNode(val){this.val=val;this.next=null;}functionCreateList(...nodes){this.head=nodes[0];this.length=nodes.length;for(vari=0;i<nodes.length-1;i++){if(nodes[i+1]){nodes[i].next=nodes[i+1];}}}functionintersectNode(head1,head2){vararr1=[],arr2=[],theValue=null;transformToArray(head1,arr1);transformToArray(head2,arr2);for(vari=0;i<arr1.length;i++){if(arr2.indexOf(arr1[i])!==-1){theValue=arr1[i];return'Reference of the node with value = '+theValue;}}returntheValue;}functiontransformToArray(head,arr){while(head){arr.push(head.val);head=head.next;}}constnode1=newCreateNode(1);constnode2=newCreateNode(2);constnode3=newCreateNode(3);constnode4=newCreateNode(4);constnode5=newCreateNode(5);constnodeA=newCreateNode('A');constnodeB=newCreateNode('B');constnodeC=newCreateNode('C');constnodeD=newCreateNode('D');constnodeE=newCreateNode('E');constlist1=newCreateList(node1,node2,node3,node4,node5);constlist2=newCreateList(nodeA,nodeB,node3,nodeC,nodeD);intersectNode(list1.head,list2.head);
functionintersectNode(head1,head2){vararr1=[],arr2=[],theValue=null;transformToArray(head1,arr1);transformToArray(head2,arr2);for(vari=0;i<arr1.length;i++){if(arr2.indexOf(arr1[i])!==-1){theValue=arr1[i];return'Reference of the node with value = '+theValue;}}returntheValue;/* arr1.some( value => { if ( arr2.includes(value) ){ theValue = value; return true; } }) if( theValue ){ return 'Reference of the node with value = ' + theValue; } return null; */}functiontransformToArray(head,arr){while(head){arr.push(head.val);head=head.next;}}
测试代码:
functionCreateNode(val){this.val=val;this.next=null;}functionCreateList(...nodes){this.head=nodes[0];this.length=nodes.length;for(vari=0;i<nodes.length-1;i++){if(nodes[i+1]){nodes[i].next=nodes[i+1];}}}functionintersectNode(head1,head2){vararr1=[],arr2=[],theValue=null;transformToArray(head1,arr1);transformToArray(head2,arr2);for(vari=0;i<arr1.length;i++){if(arr2.indexOf(arr1[i])!==-1){theValue=arr1[i];return'Reference of the node with value = '+theValue;}}returntheValue;}functiontransformToArray(head,arr){while(head){arr.push(head.val);head=head.next;}}constnode1=newCreateNode(1);constnode2=newCreateNode(2);constnode3=newCreateNode(3);constnode4=newCreateNode(4);constnode5=newCreateNode(5);constnodeA=newCreateNode('A');constnodeB=newCreateNode('B');constnodeC=newCreateNode('C');constnodeD=newCreateNode('D');constnodeE=newCreateNode('E');constlist1=newCreateList(node1,node2,node3,node4,node5);constlist2=newCreateList(nodeA,nodeB,node3,nodeC,nodeD);intersectNode(list1.head,list2.head);
Activity
[-]字节leetcode160:相交链表[/-][+]字节&leetcode160:相交链表[/+][-]字节&leetcode160:相交链表[/-][+]字节&leetcode160:编写一个程序,找到两个单链表相交的起始节点[/+]hh-cheng commentedon Apr 14, 2020
方法:遍历
测试代码:
hecun0000 commentedon Apr 15, 2020
sisterAn commentedon Apr 15, 2020
解法一:标记法(简单但空间复杂度为O(n),不符合,仅做参考)
解题思路: 两次遍历,先遍历一个链表,给链表中的每个节点都增加一个标志位,然后遍历另外一个链表,遍历到第一个已被标志过的节点为两链表相交的起始节点。
若遍历完都没有发现已被标志过的节点,则两链表不相交,返回
null时间复杂度:O(n)
空间复杂度:O(n)
解法二:双指针法
解题思路: 如果 A、B 两链表相交,则 A 、B 自相交点往后的链表是一致的。
我们可以尝试消除 A、B 链表的长度差,同步遍历上图中的方框里的节点,判断是否有相同节点,若有相同则是两链表相交,返回第一个相同节点 即可。否则返回
null,两链表不相交。解题步骤:
pA、pB,直到遍历完其中一个链表(短链表),如上图,设A为长链表pA到链尾的长度,此时可以把pB指向长链表的表头headA,继续同步遍历,直到遍历完长链表headA到pB的长度就为两链表的长度差,pB到链表的长度与headB到链尾的长度一致pA指向headB,然后同步遍历pB及pA,直到有相交节点,返回相交节点,否则返回null画图帮助理解:
时间复杂度:O(n)
空间复杂度:O(1)
leetcode
sisterAn commentedon Apr 15, 2020
想法不错,不过可以进阶一下,使用双指针来做,使之尽量满足空间复杂度O(1)
[-]字节&leetcode160:编写一个程序,找到两个单链表相交的起始节点[/-][+]图解字节&leetcode160:编写一个程序,找到两个单链表相交的起始节点[/+]cs739295 commentedon Jun 15, 2020
你好,我想问一下,if(pA === pB) return pA。这行代码怎么理解?题目表示是两个单独的链表,链表里的节点都是对象,对象的这种===会有效吗?他们两个好像是指向不同的地址空间把。是不是应该比较pA与pB的值才行。改为if(pA.val===pB) return pA;会不会更好一点?
Been101 commentedon Jul 18, 2020
AnranS commentedon Nov 17, 2020
WeakMap
双指针
xiaoerwen commentedon Apr 8, 2021
JohnApache commentedon Apr 27, 2021
我觉得
双指针消除高度差的方案, 时间复杂度并不是On, 在 极限差的情况下[4,1,8,4,5],[5,0,1,8,4,5],假如相交的节点是最后一个点,会出现 遍历两遍链表的情况, 。我自己实现了的方案,也是
双指针,但是最多只会遍历一次,不会出现遍历两遍的情况,