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,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节点。

public class Solution {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null) {
            return null;
        }
        int lengthA = 1;
        int lengthB = 1;

        ListNode iterA = headA;
        while(iterA.next!=null) {
            iterA = iterA.next;
            lengthA++;
        }

        ListNode iterB = headB;
        while(iterB.next!=null) {
            iterB = iterB.next;
            lengthB++;
        }

        if(iterA!=iterB) {
            return null;
        }

        if(lengthA>lengthB) {
            int tmp = lengthA - lengthB;
            while(tmp>0) {
                headA = headA.next;
                tmp--;
            }
        } else {
            int tmp = lengthB - lengthA;
            while(tmp>0) {
                headB = headB.next;
                tmp--;
            }
        }

        while(headA!=headB) {
            headA = headA.next;
            headB = headB.next;
        }

        return headA;
    }
}

时间: 2024-11-13 20:57:04

LeetCode Intersection of Two Linked Lists 解题报告的相关文章

【原创】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] 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 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

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

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 两链表是否相交

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: 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: 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】Merge k Sorted Lists 解题报告

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 合并几个有序链表为一个,分析算法复杂度. [分治] 直观的想法是两两合并,有两种方法:1)list1和list2合并为newlist2,newlist2再和list3合并为newlist3,newlist3再和list4合并为newlist4--依次类推:2)list1和list2合并为li