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

提示:

假设两个链表有合并的情况,那么合并部分的长度一定是一样的,在合并之前长度会有所不同,所以先求出长度,然后把长的链表向前走,让他们“在同一起跑线”,然后依次比较。

代码:

/**
 * 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) {
        if (!headA || !headB) return NULL;
        int lenA = 0, lenB = 0;
        ListNode *nodeA, *nodeB;
        for (nodeA = headA; nodeA; nodeA = nodeA->next, ++lenA);
        for (nodeB = headB; nodeB; nodeB = nodeB->next, ++lenB);
        if (lenB > lenA) {
            for (int i = 0; i < lenB - lenA; ++i, headB = headB->next);
        } else {
            for (int i = 0; i < lenA - lenB; ++i, headA = headA->next);
        }
        if (headA == headB) return headA;
        while (headA && headB) {
            headA = headA->next;
            headB = headB->next;
            if (headA == headB) return headA;
        }
        return NULL;
    }
};
时间: 2024-10-10 13:40:04

【LeetCode】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

【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)

题目地址: https://oj.leetcode.com/problems/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 →

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

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从零单排】No.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 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 no i

【LeetCode】Flatten Binary Tree to Linked List 解题报告

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 [解析] 题意是把一棵二叉树按照先序遍历的方式放到一棵只有右支树的二叉树中. 最开始想的思路是递归发,后来发现这样会溢出. 然后就用一个栈和一个队列来实现,队列用来存储先序遍历的结果,栈用于先序遍历.