|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.ListNode;
|
4 | 4 |
|
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Set; |
| 7 | + |
5 | 8 | /**160. Intersection of Two Linked Lists
|
6 | 9 | *
|
7 | 10 | * Write a program to find the node at which the intersection of two singly linked lists begins.
|
|
21 | 24 | If the two linked lists have no intersection at all, return null.
|
22 | 25 | The linked lists must retain their original structure after the function returns.
|
23 | 26 | You may assume there are no cycles anywhere in the entire linked structure.
|
24 |
| - Your code should preferably run in O(n) time and use only O(1) memory.*/ |
| 27 | + Your code should preferably run in O(n) time and use only O(1) memory. |
| 28 | + */ |
| 29 | + |
25 | 30 | public class _160 {
|
26 |
| - /**credit: https://discuss.leetcode.com/topic/5492/concise-java-solution-o-1-memory-o-n-time*/ |
27 |
| - public ListNode getIntersectionNode(ListNode headA, ListNode headB) { |
28 |
| - int lenA = findLen(headA), lenB = findLen(headB); |
29 |
| - /**align headA and headB to the same starting point and then move together until we find the intersection point*/ |
30 |
| - while (lenA < lenB){ |
31 |
| - headB = headB.next; |
32 |
| - lenB--; |
33 |
| - } |
34 |
| - |
35 |
| - while (lenB < lenA){ |
36 |
| - headA = headA.next; |
37 |
| - lenA--; |
| 31 | + |
| 32 | + public static class Solution1 { |
| 33 | + |
| 34 | + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { |
| 35 | + int lenA = findLen(headA), lenB = findLen(headB); |
| 36 | + /**align headA and headB to the same starting point and then move together until we find the intersection point*/ |
| 37 | + while (lenA < lenB) { |
| 38 | + headB = headB.next; |
| 39 | + lenB--; |
| 40 | + } |
| 41 | + |
| 42 | + while (lenB < lenA) { |
| 43 | + headA = headA.next; |
| 44 | + lenA--; |
| 45 | + } |
| 46 | + |
| 47 | + while (headA != headB) { |
| 48 | + headA = headA.next; |
| 49 | + headB = headB.next; |
| 50 | + } |
| 51 | + |
| 52 | + return headA; |
38 | 53 | }
|
39 |
| - |
40 |
| - while (headA != headB){ |
41 |
| - headA = headA.next; |
42 |
| - headB = headB.next; |
| 54 | + |
| 55 | + private int findLen(ListNode head) { |
| 56 | + int len = 0; |
| 57 | + while (head != null) { |
| 58 | + head = head.next; |
| 59 | + len++; |
| 60 | + } |
| 61 | + return len; |
43 | 62 | }
|
44 |
| - |
45 |
| - return headA; |
| 63 | + |
46 | 64 | }
|
47 |
| - |
48 |
| - private int findLen(ListNode head){ |
49 |
| - int len = 0; |
50 |
| - while (head != null){ |
51 |
| - head = head.next; |
52 |
| - len++; |
| 65 | + |
| 66 | + public static class Solution2 { |
| 67 | + /** |
| 68 | + * O(m+n) time |
| 69 | + * O(1) space |
| 70 | + * credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len*/ |
| 71 | + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { |
| 72 | + if (headA == null || headB == null) return null; |
| 73 | + |
| 74 | + ListNode a = headA; |
| 75 | + ListNode b = headB; |
| 76 | + |
| 77 | + while (a != b) { |
| 78 | + a = a == null ? headB : a.next; |
| 79 | + b = b == null ? headA : b.next; |
| 80 | + } |
| 81 | + return a; |
53 | 82 | }
|
54 |
| - return len; |
55 | 83 | }
|
56 | 84 |
|
| 85 | + public static class Solution3 { |
| 86 | + /** |
| 87 | + * O(m+n) time |
| 88 | + * O(Math.max(m, n)) space |
| 89 | + * */ |
| 90 | + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { |
| 91 | + Set<ListNode> set = new HashSet<>(); |
| 92 | + while (headA != null) { |
| 93 | + set.add(headA); |
| 94 | + headA = headA.next; |
| 95 | + } |
| 96 | + |
| 97 | + while (headB != null) { |
| 98 | + if (set.contains(headB)) return headB; |
| 99 | + headB = headB.next; |
| 100 | + } |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
57 | 104 | }
|
0 commit comments