√双指针法: 将两个链表的非公共部分加成一样长(b+a=a+b)
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
ListNode *ta = pHead1, *tb = pHead2;
while (ta != tb) {
ta = ta ? ta->next : pHead2;//能在第一遍遍历时检测链表是否在头部重合
tb = tb ? tb->next : pHead1;//否则能在第二遍遍历时校齐
}
return ta;
}
};
×差值法:让长的链表先走个差值 这种方法对{1,2,3,5},{1,2,4},{1,2}的示例不适用 ×逆转链表:公共节点无法逆转