leetcode 日经贴,Cpp code -Intersection of Two Linked Lists
2015-04-22 16:16
288 查看
Intersection of Two Linked Lists
/** * 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 *ca = headA, *cb = headB; int len1 = 0, len2 = 0; while (ca && ca->next) { ++len1; ca = ca->next; } while (cb && cb->next) { ++len2; cb = cb->next; } if (!ca || !cb || ca != cb) { return NULL; } ca = headA; cb = headB; while (len1 > len2) { ca = ca->next; --len1; } while (len2 > len1) { cb = cb->next; --len2; } while (ca != cb) { ca = ca->next; cb = cb->next; } return ca; } };
相关文章推荐
- [leetcode 160] Intersection of Two Linked Lists
- 【leetcode 两个链表的交集点】Intersection of Two Linked Lists
- LeetCode - 160 - Intersection of Two Linked Lists
- leetcode-160-Intersection of Two Linked Lists
- [Leetcode 160, Easy] Intersection of Two Linked Lists
- Intersection of Two Linked Lists - LeetCode
- Leetcode Intersection of Two Linked Lists
- 【Leetcode】Intersection of Two Linked Lists
- leetcode 160-Intersection of Two Linked Lists
- LeetCode(160) Intersection of Two Linked Lists
- [leetcode] [leetcode]Intersection of Two Linked Lists
- [Leetcode 160] Intersection of Two Linked Lists
- LeetCode: Intersection of Two Linked Lists
- [leetcode]Intersection of Two Linked Lists
- Leetcode: Intersection of Two Linked Lists
- Leetcode - Intersection of Two Linked Lists
- Leetcode Intersection of Two Linked Lists
- 【leetcode】Intersection of Two Linked Lists(easy)
- LeetCode 160 Intersection of Two Linked Lists(链表相交)(*)
- [leetcode 160]Intersection of Two Linked Lists