-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindFirstCommonListNode.java
47 lines (43 loc) · 1.07 KB
/
FindFirstCommonListNode.java
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
42
43
44
45
46
47
package SwordToOffer;
import java.util.Stack;
/**
* Created by tlh on 2017/3/19.
*/
public class FindFirstCommonListNode {
private class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if (pHead1 == null || pHead2 == null)
return null;
Stack<ListNode> s1 = new Stack<>();
Stack<ListNode> s2 = new Stack<>();
ListNode p = pHead1;
while (p != null) {
s1.push(p);
p = p.next;
}
p = pHead2;
while (p != null) {
s2.push(p);
p = p.next;
}
if (s1.peek() != s2.peek())
return null;
ListNode p1 = null;
ListNode p2 = null;
while (true) {
p1 = s1.pop();
p2 = s2.pop();
if (p1 != p2)
break;
if (s1.isEmpty() || s2.isEmpty())
return p1;
}
return p1.next;
}
}