LintCode-劍指Offer-(380)兩個鏈表的交叉

/**
 * 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
        ListNode* tmpb = headB;
        ListNode* tmpa = headA;
        while (tmpa!=NULL){
            tmpb = headB;
            while (tmpb!=NULL){
                if (tmpa->val!=tmpb->val){
                    tmpb = tmpb->next;;
                }
                else{
                    return tmpa;
                }
            }
            tmpa = tmpa->next;
        }
        return NULL;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章