-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_160.java
More file actions
57 lines (49 loc) · 1.24 KB
/
Copy pathleetCode_160.java
File metadata and controls
57 lines (49 loc) · 1.24 KB
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
48
49
50
51
52
53
54
55
56
57
/**
* 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) {
if(headA == null || headB == null) return null;
int lenA = len(headA);
int lenB = len(headB);
ListNode tempA = headA;
ListNode tempB = headB;
if(lenA > lenB){
while(lenA > lenB){
tempA= tempA.next;
lenA--;
}
}else if(lenB > lenA){
while(lenB > lenA){
tempB = tempB.next;
lenB--;
}
}
while(tempA != null && tempB !=null){
if(tempA == tempB) return tempA;
tempA = tempA.next;
tempB = tempB.next;
}
return null;
}
public int len(ListNode head){
if(head == null){
return 0;
}
int res=0;
ListNode temp = head;
while(temp != null){
res++;
temp=temp.next;
}
return res;
}
}