[leetcode] 160. Intersection of Two Linked Lists

441 篇文章 0 订阅
284 篇文章 0 订阅

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:
two links
begin to intersect at node c1.

Example 1:
example1

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:
example2

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:
example3

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

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.

分析

题目的意思是:找到两个链表的交汇点。

  • 常规的方法是用快慢指针来解决。这里使用的不一样的方法来解决这个问题,读者有兴趣可以模拟一遍这个解法,很巧妙的利用了两个链表原来的头结点指针。比如l1走到尾部了,我们让对方的头指针接着走,直到走到l2到尾部为止。

代码

/**
 * 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) {
        ListNode *l1=headA;
        ListNode *l2=headB;
        while(l1||l2){
            if(l1){
                l1=l1->next;
            }else{
                headB=headB->next;
            }
            if(l2){
                l2=l2->next;
            }else{
                headA=headA->next;
            }
        }
        while(headA!=headB){
            headA=headA->next;
            headB=headB->next;
        }
        return headA;
    }
};

代码二

  • 找出两个链表的第一个公共节点,我重新实现了一下,求出两个链表的长度,然后把长的链表对齐到短的链表,然后一起遍历找到公共节点就行了。
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#
# 
# @param pHead1 ListNode类 
# @param pHead2 ListNode类 
# @return ListNode类
#
class Solution:
    def FindFirstCommonNode(self , pHead1 , pHead2 ):
        # write code here
        
        p1=pHead1
        p2=pHead2
        
        cnt1=0
        while(p1):
            cnt1+=1
            p1=p1.next
        cnt2=0
        while(p2):
            cnt2+=1
            p2=p2.next
        while(cnt1>cnt2):
            pHead1=pHead1.next
            cnt1-=1
        while(cnt1<cnt2):
            pHead2=pHead2.next
            cnt2-=1
        while(pHead1 and pHead2 and pHead1.val!=pHead2.val):
            pHead1=pHead1.next
            pHead2=pHead2.next
        return pHead1

参考文献

160. Intersection of Two Linked Lists

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值