日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[LeetCode]--160. Intersection of Two Linked Lists

發(fā)布時(shí)間:2025/4/16 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [LeetCode]--160. Intersection of Two Linked Lists 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A: a1 → a2↘c1 → c2 → c3↗ B: b1 → b2 → b3

begin to intersect at node c1.

Notes:

If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.

因?yàn)槭苌厦嬉粋€(gè)問題影響Java單鏈表逆轉(zhuǎn)所以我想著先逆轉(zhuǎn)后求后面的子串,其實(shí)想多了,只要有一個(gè)公共的就當(dāng)時(shí)后面的全部是公共。

解法一:
第一遍循環(huán),找出兩個(gè)鏈表的長度差N
第二遍循環(huán),長鏈表先走N步,然后同時(shí)移動(dòng),判斷是否有相同節(jié)點(diǎn)
解法一就相對(duì)簡單粗暴,容易理解。

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA==null || headB==null) return null; ListNode p = headA; ListNode q = headB; int pcount = 0; int qcount = 0; while(p.next != null || q.next != null) { if(p == q) return p; if(p.next != null) p = p.next; else ++qcount; if(q.next != null) q = q.next; else ++pcount; } if(p != q) return null; p = headA; q = headB; while(pcount-- != 0) { p = p.next; } while(qcount-- != 0) { q = q.next; } while(p != q) { p = p.next; q = q.next; } return p; } }

解法二:
鏈表到尾部后,跳到另一個(gè)鏈表的頭部, 另外一個(gè)鏈表的引用移到最后,此時(shí)跳過來的引用所在點(diǎn)就為第二個(gè)鏈表的開始點(diǎn),然后再次一起遍歷,相遇即為intersection points.

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA==null || headB==null) return null; ListNode p = headA; ListNode q = headB; if(p == q) return p; while(p!=null && q!=null) { p = p.next; q = q.next; } if(p==null) p = headB; else q = headA; while(p!=null && q!=null) { p = p.next; q = q.next; } if(p==null) p = headB; else q = headA; while(p!=null && q!=null) { if(p==q) return p; p = p.next; q = q.next; } return null; } }

就是先一起移動(dòng),如果一個(gè)到頭了,就指向另一個(gè)的頭,另一個(gè)到尾了,就說明這個(gè)指向的就是下次一起開始的地方。

解法三:
用兩個(gè)引用,一個(gè)快一個(gè)慢,快的每次移動(dòng)兩個(gè)節(jié)點(diǎn),當(dāng)。。。。。。。。。。。我以為我理解了,其實(shí)沒理解,然后去打斷點(diǎn)調(diào)試就發(fā)現(xiàn)出問題了,不過leetcode上面顯示Accept!很奇怪,先貼個(gè)代碼,等以后問問別人。

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {if (headA == null || headB == null) {return null;}// get the tail of list A.ListNode node = headA;while (node.next != null) {node = node.next;}node.next = headB;ListNode result = listCycleII(headA);node.next = null;return result;}private ListNode listCycleII(ListNode head) {ListNode slow = head, fast = head.next;while (slow != fast) {if (fast == null || fast.next == null) {return null;}slow = slow.next;fast = fast.next.next;}slow = head;fast = fast.next;while (slow != fast) {slow = slow.next;fast = fast.next;}return slow;}

總結(jié)

以上是生活随笔為你收集整理的[LeetCode]--160. Intersection of Two Linked Lists的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。