-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionTwoLinkedLists_160.java
More file actions
36 lines (30 loc) · 1.06 KB
/
IntersectionTwoLinkedLists_160.java
File metadata and controls
36 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
/**
* 1. This is about quick-slow linkedList pointer problem
* you can also cal each length of the linkedList, and then move from the longlen - shortlen poisition * from longer linkedlist
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode p1 = headA; ListNode p2 = headB;
while (p1 != null && p2 != null) { p1 = p1.next; p2 = p2.next; }
p1 = (p1 == null) ? headB : p1;
p2 = (p2 == null) ? headA : p2;
while (p1 != null && p2 != null) { p1 = p1.next; p2 = p2.next; }
p1 = (p1 == null) ? headB : p1;
p2 = (p2 == null) ? headA : p2;
// p1 & p2 are now same length
while (p1 != null && p1 != p2) { p1 = p1.next; p2 = p2.next; }
return p1;
}
}