[Leetcode] Intersection of Two Linked Lists
2014-11-28 06:53
423 查看
class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if (headA == NULL || headB == NULL) return NULL; int lenA = 0; int lenB = 0; ListNode *curA = headA; ListNode *curB = headB; while (curA->next) { lenA++; curA = curA->next; } while(curB->next) { lenB++; curB = curB->next; } //lenA++; //lenB++; if (curA != curB) return NULL; int i; if (lenA > lenB) { for(i = lenA-lenB; i; i--) headA = headA->next; } else { for(i = lenB-lenA; i; i--) headB = headB->next; } for (i = 0; i <= min(lenA, lenB); i++) { if (headA == headB) return headA; headA = headA->next; headB = headB->next; } return NULL; } };
相关文章推荐
- 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
- LeetCode[Linked List]: Intersection of Two Linked Lists
- [LeetCode]Intersection of Two Linked Lists
- Leetcode-Intersection of Two Linked Lists
- Intersection of Two Linked Lists--leetcode
- 【Leetcode】Intersection of Two Linked Lists in JAVA
- 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 两链表是否相交
- [Leetcode]Intersection of Two Linked Lists
- 和大神们学习每天一题(leetcode)-Intersection of Two Linked Lists
- Leetcode_160_Intersection of Two Linked Lists