【leetcode】Intersection of Two Linked Lists(easy)

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.

思路:简单题 遍历长度 走差值 再同步走   答案里给了个转圈圈的思路 感觉没有自己的好 太乱了

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA == NULL || headB == NULL)
        {
            return NULL;
        }
        int lenA = 1;
        int lenB = 1;
        ListNode * pa = headA;
        ListNode * pb = headB;

        while(pa->next != NULL)
        {
            pa = pa->next;
            lenA++;
        }
        while(pb->next != NULL)
        {
            pb = pb->next;
            lenB++;
        }

        if(pa != pb) return NULL;

        pa = headA;
        pb = headB;
        while(lenA > lenB)
        {
            pa = pa->next; lenA--;
        }
        while(lenB > lenA)
        {
            pb = pb->next; lenB--;
        }
        while(pa != pb)
        {
            pa = pa->next;
            pb = pb->next;
        }

    }
时间: 2025-01-02 08:38:39

【leetcode】Intersection of Two Linked Lists(easy)的相关文章

【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:

【Leetcode】Intersection of Two Linked Lists in JAVA

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. 还是倒着想,把listnode放进栈里,然后一个一个pop出来,直到不一样的话,

LeetCode【160】Intersection of Two Linked Lists

For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 思路比较清晰,首先确定二者的长度并计算长度之差的绝对值dis,让较长链表先走dis步,然后两个指针一起走.如果两个指针相等,则返回,否则当指针达到NULL时,没有交点. AC代码如下: void getLength(ListNode *head , int& len)

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

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每天一题】 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][003] 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: 1. If the two linke

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

[C++]LeetCode: 60 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