{"id":910,"date":"2025-10-25T18:56:40","date_gmt":"2025-10-25T10:56:40","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=910"},"modified":"2025-10-25T18:56:40","modified_gmt":"2025-10-25T10:56:40","slug":"leetcode-160-%e7%9b%b8%e4%ba%a4%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=910","title":{"rendered":"LeetCode 160 \u2013 \u76f8\u4ea4\u94fe\u8868"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e24\u4e2a\u5355\u94fe\u8868\u7684\u5934\u8282\u70b9 headA \u548c headB \uff0c\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u4e24\u4e2a\u5355\u94fe\u8868\u76f8\u4ea4\u7684\u8d77\u59cb\u8282\u70b9\u3002\u5982\u679c\u4e24\u4e2a\u94fe\u8868\u4e0d\u5b58\u5728\u76f8\u4ea4\u8282\u70b9\uff0c\u8fd4\u56de null \u3002<\/p>\n<p>\u56fe\u793a\u4e24\u4e2a\u94fe\u8868\u5728\u8282\u70b9 c1 \u5f00\u59cb\u76f8\u4ea4\uff1a<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_8f315713a24884325195275e40925723.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_8f315713a24884325195275e40925723.jpg\" alt=\"\" \/><\/a><br \/>\n\u9898\u76ee\u6570\u636e \u4fdd\u8bc1 \u6574\u4e2a\u94fe\u5f0f\u7ed3\u6784\u4e2d\u4e0d\u5b58\u5728\u73af\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u540e\uff0c\u94fe\u8868\u5fc5\u987b \u4fdd\u6301\u5176\u539f\u59cb\u7ed3\u6784 \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u957f\u5ea6\u5dee<\/h2>\n<p>\u5148\u904d\u5386\u4e00\u4e0b\u4e24\u4e2a\u94fe\u8868\u7684\u957f\u5ea6\uff0c\u5dee\u4e3a count\u3002<br \/>\n\u8ba9\u957f\u7684\u5148\u8d70 count \u6b65\uff0c\u6700\u540e\u518d\u540c\u6b65\u8d70\uff0c\u76f4\u5230\u76f8\u9047\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(m + n)<\/p>\n<h2>\u62fc\u63a5<\/h2>\n<p>\u8ba9\u4e24\u4e2a\u94fe\u8868\u76f8\u4e92\u62fc\u63a5\uff0c\u5f97\u5230 A+B \u548c B+A\u3002<br \/>\n\u6b64\u65f6\u957f\u5ea6\u662f\u76f8\u540c\u7684\uff0c\u53ef\u4ee5\u540c\u6b65\u8fdb\u884c\u904d\u5386\u3002<br \/>\n\uff08\u4ee3\u7801\u4e2d\u4e0d\u9700\u8981\u771f\u7684\u505a\u62fc\u63a5\uff0c\u624b\u52a8\u63a7\u5236\u6307\u9488\u7684\u904d\u5386\u987a\u5e8f\u5373\u53ef\uff09<br \/>\n\u5982\u679c\u94fe\u8868\u76f8\u4ea4\uff0c\u4f1a\u76f8\u9047\uff1b\u5982\u679c\u4e0d\u76f8\u4ea4\uff0c\u4f1a\u540c\u65f6\u4e3anull\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6 O(m + n)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {\n    int lengthA = getLength(headA);\n    int lengthB = getLength(headB);\n\n    \/\/ \u786e\u4fdd A \u6bd4\u8f83\u957f\n    if (lengthB &gt; lengthA) {\n        ListNode temp = headB;\n        headB = headA;\n        headA = temp;\n    }\n    int count = Math.abs(lengthA - lengthB);\n    while (count &gt; 0) {\n        headA = headA.next;\n        count--;\n    }\n    while (headA != headB) {\n        headA = headA.next;\n        headB = headB.next;\n    }\n\n    return headA;\n}\n\nprivate int getLength(ListNode node) {\n    int length = 0;\n    while (node != null) {\n        length++;\n        node = node.next;\n    }\n    return length;\n}\n\npublic ListNode getIntersectionNode2(ListNode headA, ListNode headB) {\n    ListNode a = headA, b = headB;\n    while(a != b) {\n        a = a == null ? headB : a.next;\n        b = b == null ? headA : b.next;\n    }\n\n    return a;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun getIntersectionNode(headA: ListNode?, headB: ListNode?): ListNode? {\n    var a = headA\n    var b = headB\n    while (a != b) {\n        a = if (a == null) headB else a.next\n        b = if (b == null) headA else b.next\n    }\n    return a\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e24\u4e2a\u5355\u94fe\u8868\u7684\u5934\u8282\u70b9 headA \u548c headB \uff0c\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u4e24\u4e2a\u5355\u94fe\u8868\u76f8\u4ea4\u7684\u8d77\u59cb\u8282\u70b9\u3002\u5982\u679c\u4e24 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,75,84],"class_list":["post-910","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-75","tag-84"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=910"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":911,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/910\/revisions\/911"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}