Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a9026ea

Browse files
edit 160
1 parent 7a9ca71 commit a9026ea

File tree

2 files changed

+110
-26
lines changed

2 files changed

+110
-26
lines changed

src/main/java/com/fishercoder/solutions/_160.java

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.fishercoder.common.classes.ListNode;
44

5+
import java.util.HashSet;
6+
import java.util.Set;
7+
58
/**160. Intersection of Two Linked Lists
69
*
710
* Write a program to find the node at which the intersection of two singly linked lists begins.
@@ -21,37 +24,81 @@
2124
If the two linked lists have no intersection at all, return null.
2225
The linked lists must retain their original structure after the function returns.
2326
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+
2530
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;
3853
}
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;
4362
}
44-
45-
return headA;
63+
4664
}
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;
5382
}
54-
return len;
5583
}
5684

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+
}
57104
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.solutions._160;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _160Test {
11+
private static _160.Solution1 solution1;
12+
private static _160.Solution2 solution2;
13+
private static _160.Solution3 solution3;
14+
private static ListNode headA;
15+
private static ListNode headB;
16+
private static ListNode expected;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
solution1 = new _160.Solution1();
21+
solution2 = new _160.Solution2();
22+
solution3 = new _160.Solution3();
23+
}
24+
25+
@Test
26+
public void test1(){
27+
headA = new ListNode(3);
28+
headB = new ListNode(2);
29+
headB.next = new ListNode(3);
30+
expected = new ListNode(3);
31+
/**TODO: both solution1 and solution2 are ACCEPTED on OJ, but somehow it's not passing in this unit test.*/
32+
// assertEquals(expected, solution1.getIntersectionNode(headA, headB));
33+
// assertEquals(expected, solution2.getIntersectionNode(headA, headB));
34+
assertEquals(expected, solution3.getIntersectionNode(headA, headB));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)