[leetcode] 160. Intersection of Two Linked Lists
原创
©著作权归作者所有:来自51CTO博客作者是念的原创作品,请联系作者获取转载授权,否则将追究法律责任
Description
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.Example 1:
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,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,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
Example 2:
Input: intersectVal = 2, listA = [0,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 [0,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.
Example 3:
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:
- If the two linked lists have no intersection at all, return null.
- The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
分析
题目的意思是:找到两个链表的交汇点。
- 常规的方法是用快慢指针来解决。这里使用的不一样的方法来解决这个问题,读者有兴趣可以模拟一遍这个解法,很巧妙的利用了两个链表原来的头结点指针。比如l1走到尾部了,我们让对方的头指针接着走,直到走到l2到尾部为止。
代码
/**
* 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) {
ListNode *l1=headA;
ListNode *l2=headB;
while(l1||l2){
if(l1){
l1=l1->next;
}else{
headB=headB->next;
}
if(l2){
l2=l2->next;
}else{
headA=headA->next;
}
}
while(headA!=headB){
headA=headA->next;
headB=headB->next;
}
return headA;
}
};
代码二
- 找出两个链表的第一个公共节点,我重新实现了一下,求出两个链表的长度,然后把长的链表对齐到短的链表,然后一起遍历找到公共节点就行了。
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param pHead1 ListNode类
# @param pHead2 ListNode类
# @return ListNode类
#
class Solution:
def FindFirstCommonNode(self , pHead1 , pHead2 ):
# write code here
p1=pHead1
p2=pHead2
cnt1=0
while(p1):
cnt1+=1
p1=p1.next
cnt2=0
while(p2):
cnt2+=1
p2=p2.next
while(cnt1>cnt2):
pHead1=pHead1.next
cnt1-=1
while(cnt1<cnt2):
pHead2=pHead2.next
cnt2-=1
while(pHead1 and pHead2 and pHead1.val!=pHead2.val):
pHead1=pHead1.next
pHead2=pHead2.next
return pHead1
参考文献
160. Intersection of Two Linked Lists