-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
42 lines (41 loc) · 1.13 KB
/
Main.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
public class IntersectionofTwoLinkedLists160 {
/**
* 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 la = listLength(headA);
int lb = listLength(headB);
if (la < lb) {
ListNode temp = headA; headA = headB; headB = temp;
int t = la; la = lb; lb = t;
}
// la > lb;
for (int i = 0; i < (la - lb); i++) {
headA = headA.next;
}
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}
public int listLength (ListNode head) {
int l = 0;
ListNode pivot = head;
while (pivot != null) {
l++;
pivot = pivot.next;
}
return l;
}
}
}