LeetCode OJ 160. Intersection of Two Linked Lists

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.

这个题目是要找出两个链表的交叉点,该如何解决呢?如果两个链表相交,我们从A链表出发移动一段距离alen,出B列表出发移动一段距离blen,那么会发现他们指向同一个节点c1。那么这个距离是多少呢?

我们把每一个链表看成两段,不相交的一段和相交的一段。相交的一段对于两个链表长度是一样的,不想交的一段链表的长度是不同的。如果我们分别计算出两个链表的长度,然后计算他们长度的差值f,然后在较长的链表上先移动距离f,再同时从A,B链表出发开始遍历,若发现他们指向同一个节点,则他们相交并返回相交的点,若他们不想交,则会遍历到链表的尾部,则返回null。代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14         ListNode p1 = headA, p2 = headB;
15         int len1 = 0, len2 = 0;
16         while (p1 != null) {    //求链表A的长度
17             p1 = p1.next;
18             len1++;
19         }
20         while (p2 != null) {    //求链表B的长度
21             p2 = p2.next;
22             len2++;
23         }
24         p1 = headA;
25         p2 = headB;
26         if (len1 > len2) {      //计算链表长度的差值并在较长的链表上向后移动|len1-len2|
27             for (int i = 0;i < len1 - len2; i++) {
28                 p1 = p1.next;
29             }
30         } else {
31             for (int i = 0;i < len2 - len1; i++) {
32                 p2 = p2.next;
33             }
34         }
35         while (p1 != p2) {      //向后遍历链表A和链表B,找到相交的节点,若遍历到最后,返回null
36             p1 = p1.next;
37             p2 = p2.next;
38         }
39         return p1;
40     }
41 }
时间: 2024-11-07 09:50:39

LeetCode OJ 160. Intersection of Two Linked Lists的相关文章

【一天一道LeetCode】#160. Intersection of Two Linked Lists

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

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. 找出第一个合并的节点的位置(或者说插入),注意复杂度为O(N),那么先遍历两个链

【LeetCode】160. Intersection of Two Linked Lists

题目: 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

[LeedCode OJ]#160 Intersection of Two Linked Lists

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/intersection-of-two-linked-lists/ 题意: 给定两个链表,要求找出这两个链表的交点 思路: 我们可以设定两个指针,分别遍历得到a,b的长度,然后如果a长,就将a的指针从头指针往下移动k位直到与b指针等长的位置,b链表长也是如此. 然后两个指针同时出发,一旦走到相同的位置,那么这个位置就是两个

leetCode 160. Intersection of Two Linked Lists 链表

160. Intersection of Two Linked Lists 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

160. Intersection of Two Linked Lists(js)

160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA

[LeetCode] 160. Intersection of Two Linked Lists 解题思路

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 i

Leetcode 160 Intersection of Two Linked Lists(快慢指针)

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 i

[LeetCode 160] Intersection of Two Linked Lists

题目链接:intersection-of-two-linked-lists /** * 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