05 August 2008
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
begin to intersect at node c1.
1
2
3
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
1
2
3
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
1
2
3
4
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.
Notes:
1
null
.1
[1, 10^9]
.给两个链表,判断是否相交,如果有相交就返回相交的node,没有则相交返回null;不要改变链表的结构,链表中没有环;要求O(n)的时间复杂度和O(1)的空间复杂度。
解题思路有如下:
用办法3减length来做
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
/**
* 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 = getLength(headA), lenB = getLength(headB);
if (lenA > lenB) { //如果headA长,让headA从长度相当于headB的长度开始
for (int i = 0; i < lenA - lenB; ++i) {
headA = headA.next;
}
} else {
for (int i = 0; i < lenB- lenA; ++i) {//反之从另外一个链表开始
headB = headB.next;
}
}
while (headA != null && headB != null && headA != headB){//挨个比较,看什么时候相同
headA = headA.next;
headB = headB.next;
}
return (headA != null && headB != null) ? headA : null; //return (headA != null && headB != null) ? headB : null;
}
public int getLength(ListNode head){
int count = 0;
while (head != null) {
count++;
head = head.next;
}
return count;
}
}
用办法4环的思想来做(做的路程一样)
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
/**
* 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;
}
ListNode a = headA;
ListNode b = headB;
//对两个链表分别遍历,直到其中一条链表到结尾;结束条件如果相交则一定会在开始重合的节点,
//如果不相交则二者都为null从而跳出循环
while (a != b) {
a = (a == null) ? headB : a.next;
b = (b == null) ? headA : b.next;
}
return a;//return b;
}
}