-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFirstCommonNode.java
More file actions
41 lines (33 loc) · 908 Bytes
/
Copy pathFindFirstCommonNode.java
File metadata and controls
41 lines (33 loc) · 908 Bytes
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
37
38
39
40
41
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
import java.util.Deque;
import java.util.LinkedList;
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
return firstCommonWithStack(pHead1, pHead2);
}
public ListNode firstCommonWithStack(ListNode h1, ListNode h2) {
Deque<ListNode> s1 = new LinkedList<>(), s2 = new LinkedList<>();
while (h1 != null) {
s1.push(h1);
h1 = h1.next;
}
while (h2 != null) {
s2.push(h2);
h2 = h2.next;
}
ListNode l1 = s1.peek();
ListNode l2 = s2.peek();
while (s1.pop() == s2.pop()) {
l1 = s1.peek();
l2 = s2.peek();
}
return l2;
}
}