160. Intersection of Two Linked Lists
来源:互联网 发布:站内优化 编辑:程序博客网 时间:2024/06/07 15:57
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.
没想到什么特别的,上hash。。空间O(N),不完美
public ListNode getIntersectionNode(ListNode headA, ListNode headB){ListNode n=headA;HashSet<ListNode> hashset=new HashSet<>(512);while(n!=null){hashset.add(n);n=n.next;}n=headB;while(n!=null){if(hashset.contains(n))return n;n=n.next;}return null;}-----------------------------------------------------------------------------------------------------
空间O(1)的,摘自:https://leetcode.com/articles/intersection-two-linked-lists/
Approach #3 (Two Pointers) [Accepted]
- Maintain two pointers pA and pB initialized at the head of A and B, respectively. Then let them both traverse through the lists, one node at a time.
- When pA reaches the end of a list, then redirect it to the head of B (yes, B, that's right.); similarly when pB reaches the end of a list, redirect it the head of A.
- If at any point pA meets pB, then pA/pB is the intersection node.
- To see why the above trick would work, consider the following two lists: A = {1,3,5,7,9,11} and B = {2,4,9,11}, which are intersected at node '9'. Since B.length (=4) < A.length (=6), pB would reach the end of the merged list first, because pB traverses exactly 2 nodes less than pA does. By redirecting pB to head A, and pA to head B, we now ask pB to travel exactly 2 more nodes than pA would. So in the second iteration, they are guaranteed to reach the intersection node at the same time.
- If two lists have intersection, then their last nodes must be the same one. So when pA/pB reaches the end of a list, record the last element of A/B respectively. If the two last elements are not the same one, then the two lists have no intersections.
Complexity Analysis
- Time complexity : O(m+n).
- Space complexity : O(1).
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { int lenA = length(headA), lenB = length(headB); // move headA and headB to the same start point while (lenA > lenB) { headA = headA.next; lenA--; } while (lenA < lenB) { headB = headB.next; lenB--; } // find the intersection until end while (headA != headB) { headA = headA.next; headB = headB.next; } return headA;}private int length(ListNode node) { int length = 0; while (node != null) { node = node.next; length++; } return length;}
0 0
- [LeetCode]160.Intersection of Two Linked Lists
- LeetCode 160. Intersection of Two Linked Lists
- 【LeetCode】160.Intersection of Two Linked Lists
- [leetcode] 160.Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- [LeetCode]160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- LeetCode--160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- LeetCode 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 【LeetCode】160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- Spring之IOC
- 聊聊高并发系统之限流特技
- 设计模式、架构与框架
- 矩阵连乘--栈 实现
- Ajax处理响应,连接 Web 表单
- 160. Intersection of Two Linked Lists
- angel arena reborn 装备合成说明
- JNIEnv 速查
- mysql数据备份
- 【Arduino官方教程第一辑】示例程序 6-2 用压电元件检测振动
- 士兵杀敌(一)nyoj
- Android源码分析之Builder模式
- 排序算法之——冒泡排序(一)
- 敏捷开发,英文是Agile,我所理解的敏捷