37-两个链表的第一个公共结点
来源:互联网 发布:php开发服务器端 编辑:程序博客网 时间:2024/06/08 13:13
一般的想法是用栈,可是超出内存了。
先把两个长的那部分截掉,然后再一一比较。
/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/class Solution {public: int GetLength(ListNode* pHead1){if (pHead1 == NULL)return 0;int count = 1;while (pHead1->next) {count++; pHead1 = pHead1->next; }return count;}ListNode* ResetStarNode(ListNode* pHead1,int dif){while (dif--)pHead1 = pHead1->next;return pHead1;}ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2){int len1 = GetLength(pHead1);int len2 = GetLength(pHead2);int dif = abs(len1 - len2);if (len1>len2){pHead1 = ResetStarNode(pHead1,dif);}elsepHead2 = ResetStarNode(pHead2,dif); while (pHead2 != NULL&&pHead1 != NULL){if (pHead1 ==pHead2) return pHead1;pHead1 = pHead1->next;pHead2 = pHead2->next;}return NULL;}};
阅读全文
0 0
- 37 两个链表的第一个公共结点
- 37两个链表的第一个公共结点
- 37-两个链表的第一个公共结点
- 找出两个链表的第一个公共结点--总结
- 找出两个链表的第一个公共结点
- 找两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 两个链表的第一个公共结点
- 两个链表的第一个公共结点
- 两个链表的第一个公共结点
- 找两个链表的第一个公共结点
- 两个链表的第一个公共结点(剑指offer37)
- Linq 联合查询
- 浅谈浏览器http的缓存机制
- 剑指offer中一些有关数组和字符串操作的代码实现和扩充
- UMG 常用方法
- 【动态规划】UVa 437 The Tower of Babylon
- 37-两个链表的第一个公共结点
- android 中处理崩溃异常
- 栈与堆栈
- Mabatis报错 java.sql.SQLException: Parameter number 3 is not an OUT parameter 解决方法
- 伪类和伪元素的区别
- Word VBA
- Codeforces B. Cards Sorting 【瞎搞】
- iOS
- 负载均衡设计Nginx+Tomcat实现负载均衡(三)