LeetCode随笔之链表
来源:互联网 发布:跟兄弟连学php 电子书 编辑:程序博客网 时间:2024/06/15 05:43
- 输入两个链表,找出它们的第一个公共结点。
/*找出2个链表的长度,然后让长的先走两个链表的长度差,然后再一起走(因为2个链表用公共的尾部)*/class Solution {public: ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { int p1Len=findLen(pHead1); int p2Len=findLen(pHead2); p1Len>p2Len? pHead1=walkStep(pHead1,p1Len-p2Len):pHead2=walkStep(pHead2,p2Len-p1Len); while(pHead1) { if(pHead1==pHead2) return pHead1; pHead1=pHead1->next;pHead2=pHead2->next; } return pHead1; } ListNode* walkStep(ListNode* pHead,int step){ while(step){pHead=pHead->next;step--;} return pHead; } int findLen(ListNode* pHead){ if(!pHead) return 0; int sum=1; while(pHead=pHead->next)sum++; return sum; }};
阅读全文
0 0
- LeetCode随笔之链表
- LeetCode随笔之Array
- LeetCode随笔之栈
- LeetCode随笔之backtracking
- LeetCode随笔之Dynamic programming
- LeetCode随笔之排序算法
- LeetCode随笔之Binary Tree(二叉树)
- LeetCode随笔之BFS广度优先搜索
- LeetCode随笔之DFS深度优先搜索
- leetcode随笔
- Leetcode 随笔
- C++链表随笔
- leetcode随笔II
- leetcode随笔III
- leetcode随笔IV
- leetcode随笔V
- leetcode随笔VI
- leetcode随笔VII
- 构造函数和复制构造函数的理解与应用
- RecyclerView——滑动监听,判断是否滑动到了最后一个item
- IOS的专利?Android也能流畅实现毛玻璃效果效果
- LA 3907 Puzzle(AC自动机)
- java实现发送邮件功能
- LeetCode随笔之链表
- Ubuntu16.04配置php5+mysql外网访问
- 常用的消息摘要算法
- 阿里云 Python课程:
- Spring基础知识汇总 Java开发必看
- 二叉树按层遍历打印换行
- 数据库和数据仓库的本质区别是什么?
- 排序
- 11.2.0.4中有严重抑郁症的DBMS_PARALLEL_EXECUTE