160. Intersection of Two Linked Lists
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
//if (!headA || !headB) return NULL; // <--
ListNode *currA = headA; // |
ListNode *currB = headB; // |
while (currA != currB) { // |
currA = currA ? currA->next : headB; //not currA->next? 会有不相交的情况
currB = currB ? currB->next : headA;
}
return currA;
}
};
naive solution
Concise JAVA solution, O(1) memory O(n) time
1, Get the length of the two lists.
2, Align them to the same start point.
3, Move them together until finding the intersection point, or the end null
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA = length(headA), lenB = length(headB);
// move headA and headB to the same start point
while (lenA > lenB) {
headA = headA.next;
lenA--;
}
while (lenA < lenB) {
headB = headB.next;
lenB--;
}
// find the intersection until end
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}
private int length(ListNode node) {
int length = 0;
while (node != null) {
node = node.next;
length++;
}
return length;
}