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.

Hide Tags

Linked List

/**
 * 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 lenA=0,lenB=0;
        ListNode *tempA=headA;
        ListNode *tempB=headB;
        while(tempA){
             lenA++;
             tempA=tempA->next;
        }
        while(tempB){
            lenB++;
            tempB=tempB->next;
        }
        while(lenA>lenB){
            headA=headA->next;
            lenA--;
        }
        while(lenB>lenA){
            headB=headB->next;
            lenB--;
        }
        while(headA != headB){
            headA=headA->next;
            headB=headB->next;
        }
        return headA;

    }
};
时间: 2024-08-09 21:05:54

Intersection of Two Linked Lists两链表找重合节点的相关文章

[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: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

[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(两个链表的入口节点)

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 = [4,1,8,4,5], listB = [5,0,1,8,4,5],

lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 在节点 c1 开始交叉. 注意 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 挑战 需满足 O(n) 时间复杂度,且仅用 O(1) 内存. 解题 尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决. 就如同上图,两个链表相交的部

160 Intersection of Two Linked Lists 相交链表

编写一个程序,找到两个单链表相交的起始节点.例如,下面的两个链表:A:           a1 → a2                                                            c1 → c2 → c3                                        B:  b1 → b2 → b3在节点 c1 开始相交.注意:    如果两个链表没有交点,返回 null.    在返回结果后,两个链表仍须保持原有的结构.    可假

[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 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

[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】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 c1. Notes: