解題思路:
(1)笨辦法,利用stack的先進後出特點
(2)從末端開始比較
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
if(pHead1==NULL || pHead2==NULL) return NULL;
stack<ListNode*> a,b;
ListNode *pre = NULL;
while(pHead1) a.push(pHead1),pHead1=pHead1->next;
while(pHead2) b.push(pHead2),pHead2=pHead2->next;
while(!a.empty() && !b.empty() && a.top()==b.top()) {
pre = a.top();
a.pop(),b.pop();
}
return pre;
}
};