【Leecode】 Instersection od Two Linked Lists
来源:互联网 发布:c 编程 猜字小游戏 编辑:程序博客网 时间:2024/06/06 23:19
/** * 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) { if(!headA || !headB) return nullptr; int aLen = 0,bLen = 0; int dis = 0; ListNode* pA = headA; ListNode* pB = headB; while(pA){ pA = pA->next; aLen++; } while(pB){ pB = pB->next; bLen++; } pA = headA;; pB = headB; if(aLen >= bLen){ dis = aLen - bLen; while(dis--){ pA = pA->next; } } if(aLen < bLen){ dis = bLen - aLen; while(dis--){ pB = pB->next; } } while(pA && pB){ if(pA == pB) return pA; pA = pA->next; pB = pB->next; } return nullptr; }};
哈哈,终于刷到第50道了!!
0 0
- 【Leecode】 Instersection od Two Linked Lists
- Leecode-21. Merge Two Sorted Lists
- Merge two sorted linked lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Leetcode_160_Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- iOS开发Provisioning profile解析
- 标准C++中的string类的用法总结
- 物理存储器和内存地址空间
- 从HolderView加载ListView异常
- switchpreference属性
- 【Leecode】 Instersection od Two Linked Lists
- Mac-关于本机-储存显示不正常
- UESTC 1712 E Easy Problem With Numbers 线段树+互质也可以求逆元
- opencv-2.4.11在vs2010中的安装
- C#基本线程同步
- Linux下清空文件内容的三种方法
- UVA 1213 Sum of Different Primes
- 使用Gulp实现实时刷新
- 136.Single Number