博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 160. Intersection of Two Linked Lists
阅读量:5245 次
发布时间:2019-06-14

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

原题链接在这里:

题目:

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.

题解:

找到length diff, 长的list head先移动diff次, 再一起移动找相同点.

Time Complexity: O(len1 + len2), len1 is the length of list one. len2 is the length of list two.

Space: O(1).

AC Java:

1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { 7  *         val = x; 8  *         next = null; 9  *     }10  * }11  */12 public class Solution {13     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {14         int len1 = length(headA);15         int len2 = length(headB);16         while(len1 > len2){17             headA = headA.next;18             len1--;19         }20         21         while(len2 > len1){22             headB =headB.next;23             len2--;24         }25         26         while(headA != headB){27             headA = headA.next;28             headB = headB.next;29         }30         31         return headA;32     }33     34     private int length(ListNode head){35         int len = 0;36         while(head != null){37             head = head.next;38             len++;39         }40         return len;41     }42 }

有一巧妙地方法来综合掉 length diff, a = headA, b = headB, a和b一起移动。当a到了list A的末位就跳到HeadB, b到了List B的末位就跳到HeadA.

等a和b相遇就是first intersection node. 因为a把first intersection node之前的list A部分, list B部分都走了一次. b也是如此. diff就综合掉了.

若是没有intersection, 那么a走到list B的结尾 null时, b正好走到 list A的结尾null, a==b. 返回了null.

Time Complexity: O(len1 + len2), len1 is the length of list one. len2 is the length of list two.

Space: O(1).

AC  Java:

1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { 7  *         val = x; 8  *         next = null; 9  *     }10  * }11  */12 public class Solution {13     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {14         ListNode a = headA;15         ListNode b = headB;16         while(a != b){17             a = a==null ? headB : a.next;18             b = b==null ? headA : b.next;19         }20         return a;21     }22 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4825010.html

你可能感兴趣的文章
空间分析开源库GEOS
查看>>
RQNOJ八月赛
查看>>
前端各种mate积累
查看>>
jQuery 1.7 发布了
查看>>
Python(软件目录结构规范)
查看>>
Windows多线程入门のCreateThread与_beginthreadex本质区别(转)
查看>>
Nginx配置文件(nginx.conf)配置详解1
查看>>
linux php编译安装
查看>>
name phone email正则表达式
查看>>
「Unity」委托 将方法作为参数传递
查看>>
重置GNOME-TERMINAL
查看>>
redis哨兵集群、docker入门
查看>>
hihoCoder 1233 : Boxes(盒子)
查看>>
oracle中anyData数据类型的使用实例
查看>>
软件测试——性能测试总结
查看>>
12.4站立会议
查看>>
客户端访问浏览器的流程
查看>>
codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
查看>>
c++||template
查看>>
[BZOJ 5323][Jxoi2018]游戏
查看>>