-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSolution.java
38 lines (38 loc) · 1 KB
/
Solution.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int a=Count(headA, 0);
int b=Count(headB, 0);
return compare(headA, a, headB, b);
}
public ListNode compare(ListNode headA,int a, ListNode headB,int b) {
if (a>b) {
return compare(headA.next, a-1, headB, b);
}else if (a<b) {
return compare(headA, a, headB.next, b-1);
}else {
if (headA==headB) {
return headA;
}else {
return compare(headA.next, a-1, headB.next, b-1);
}
}
}
public int Count(ListNode head,int i){
if (head!=null) {
i++;
i=Count(head.next,i);
}
return i;
}
}