[LeetCode] 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.

Credits:
Special thanks to @stellari for adding this problem and creating all test cases.

我还以为以后在不能免费做OJ的题了呢,想不到OJ又放出了不需要买书就能做的题,业界良心啊,哈哈^_^。这道求两个链表的交点题要求执行时间为O(n),则不能利用类似冒泡法原理去暴力查找相同点,事实证明如果链表很长的话,那样的方法效率很低。我也想到会不会是像之前删除重复元素的题一样需要用两个指针来遍历,可是想了好久也没想出来怎么弄。无奈上网搜大神们的解法,发觉其实解法很简单,因为如果两个链长度相同的话,那么对应的一个个比下去就能找到,所以只需要把长链表变短即可。具体算法为:分别遍历两个链表,得到分别对应的长度。然后求长度的差值,把较长的那个链表向后移动这个差值的个数,然后一一比较即可。代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int la = 0, lb = 0;
        ListNode *hA = headA;
        ListNode *hB = headB;
        while (headA) {
            headA = headA->next;
            ++la;
        }
        while (headB) {
            headB = headB->next;
            ++lb;
        }
        int d = la - lb;
        if (d > 0) {
            for (int i = 0; i < d; ++i) hA = hA->next;
        } else {
            for (int i = 0; i < -d; ++i) hB = hB->next;
        }
        while (hA && hB && hA != hB) {
            hA = hA->next;
            hB = hB->next;
        }
        if (hA && hB) return hA;
        else return NULL;
    }
};
时间: 2024-10-16 05:51:55

[LeetCode] Intersection of Two Linked Lists 求两个链表的交点的相关文章

[LintCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. Notice If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. Y

LeetCode—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 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——Intersection of Two Linked Lists

Description: 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. 求两个链表的相交的部分 /** * Definitio

leetcode Intersection of Two Linked Lists python

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. python

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节

[LeetCode] 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] Intersection of Two Linked Lists

Question: 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. 1.题型分类: 2.思路: 3.时间复杂度:O(n) 4.代

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 hav