标签:注意事项 linked images logs lin link adb tno img
题目:请写一个程序,找到两个单链表最开始的交叉节点。
null
。/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// write your code here
stack<ListNode *> stackA, stackB;
ListNode * result = NULL, *pA = headA, *pB = headB;
if (headA == NULL || headB == NULL)
return result;
while (pA != NULL) {
stackA.push(pA);
pA = pA->next;
}
while (pB != NULL) {
stackB.push(pB);
pB = pB->next;
}
while (!stackA.empty() && !stackB.empty()) {
if (stackA.top() == stackB.top()) {
result = stackA.top();
stackA.pop();
stackB.pop();
} else {
break;
}
}
return result;
}
};
标签:注意事项 linked images logs lin link adb tno img
原文地址:http://www.cnblogs.com/ALIMAI2002/p/7211376.html