博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
160. Intersection of Two Linked Lists(找出两个链表的交点) &&两个链表的第一个公共结点(剑指offer)...
阅读量:6271 次
发布时间:2019-06-22

本文共 3061 字,大约阅读时间需要 10 分钟。

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], skipA = 2, skipB = 3Output: Reference of the node with value = 8Input 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 = 1Output: Reference of the node with value = 2Input 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 = 2Output: nullInput 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.

 

方法一:双指针

分析:链表a的长度a+c,链表b的长度:b+c 。链表重合部分的长度是:c

那么如果指向a的指针走过长度是:a+c+b

    指向b的指针走过的长度是:b+c+a

的时候两个指针指向 的下一个结点就是第一个公共链的结点。(不然就是空节点,没有公共链)

 

时间复杂度:o(n+m)                               空间复杂度:o(1)

/*public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {        ListNode headA=pHead1;        ListNode headB=pHead2;        while (pHead1!=pHead2){            pHead1=(pHead1==null)?headB:pHead1.next;            pHead2=(pHead2==null)?headA:pHead2.next;        }        return pHead1;    }}

 方法二:利用hashset特性

hashset用于存储对象,用它存储pHead1链表所有结点。虽然它是无序的,不保证存入和取出的顺序相同,但是遍历pHead2的时候是有序的。遍历到第一个结点时就会立刻返回。

import java.util.HashSet;/*public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {        HashSet
set=new HashSet
(); while (pHead1!=null){ set.add(pHead1); pHead1=pHead1.next; } while (pHead2!=null){ if(set.contains(pHead2)) return pHead2; pHead2=pHead2.next; } return pHead2; }}

 

转载于:https://www.cnblogs.com/shaer/p/10551401.html

你可能感兴趣的文章
从反汇编的角度看引用和指针的区别
查看>>
拓马长枪定乾坤
查看>>
UIProgressView的详细使用
查看>>
Silverlight实用窍门系列:70.Silverlight的视觉状态组VisualStateGroup
查看>>
照片筛选与上传功能
查看>>
Hello ZED
查看>>
常见web攻击方式
查看>>
hdu 4472
查看>>
oracle存储过程中is和as区别
查看>>
windows 2003 群集
查看>>
几个gcc的扩展功能
查看>>
Spark一个简单案例
查看>>
关于结构体占用空间大小总结(#pragma pack的使用)
查看>>
通过浏览器查看nginx服务器状态配置方法
查看>>
shell简介
查看>>
android 使用WebView 支持播放优酷视频,土豆视频
查看>>
怎么用secureCRT连接Linux
查看>>
C# 使用WinRar命令压缩和解压缩
查看>>
linux学习笔记一----------文件相关操作
查看>>
Mono for Android 优势与劣势
查看>>