[LeetCode] 160. Intersection of Two Linked Lists

32 篇文章 0 订阅
19 篇文章 0 订阅

原题链接: https://leetcode.com/problems/intersection-of-two-linked-lists/

1. 题目介绍

Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
写一个程序去寻找两个单向链表相交的节点。比如下面这两个单向链表:

在这里插入图片描述
这两个单项链表在 c1 节点相交,我们的程序需要返回 c1 节点的引用。

Example 1:
在这里插入图片描述

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:
在这里插入图片描述

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:
在这里插入图片描述

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.
如果两个链表没有相交,那就返回null
在我们写的函数执行之后,这两个链表必须保持它们原来的结构。
你可以假设在整个链表中没有环。
时间复杂度为O(n),空间复杂度为O(1)

2. 题目思路

这道题我的第一个想法是使用集合存储已经遍历过的链表。但是空间复杂度就不是O(1)了。如果要求空间复杂度为O(1)的节点,那么很有可能是使用双指针法。

双指针法

两个指针,一个从链表A走向终点,计算出链表A的长度,另一个从链表B走向终点,计算出链表B的长度。
然后,从较长链表的第k个节点,以及较短链表的头结点开始,以相同的速度遍历两个链表,当两个链表的节点相同时,该节点就是链表相交的节点。如果一直没有相等的节点,那么就说明两个链表不交叉。

实现代码

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
        	return null;
        }
        
        ListNode curA = headA;
        ListNode curB = headB;
        int numA = countListLength(headA);
        int numB = countListLength(headB);
        
        int k = 0;
        if(numA > numB){
        	k = numA - numB;
        	//让A链表先走k步
        	while(k != 0){
        		curA = curA.next;
        		k --;
        	}
        }else{
        	k = numB - numA;
        	//让B链表先走k步
        	while(k != 0){
        		curB = curB.next;
        		k --;
        	}
        }
        
      return sameNode(curA,curB);
    }
    //计算链表的长度
    public int countListLength(ListNode head){
    	int length = 0;
    	ListNode cur = head;
    	while(cur != null){
        	length++;
        	cur = cur.next;
        }
        return length;
    }
    //寻找两个链表交叉的节点
    public ListNode sameNode(ListNode la,ListNode lb){
    	while(la != null){
    		if(la == lb){
    			return la;
    		}else{
    			la = la.next;
    			lb = lb.next;
    		}
    	}
    	return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值