leetcode Ch5-Linked List
2015-07-24 20:25
531 查看
一、
1. Remove Duplicates from Sorted List II
View Code
参考剑指offer P193. 本题说的“公共节点”不是指“值相等”,而是“同一个节点”。即两链表在该点处汇合。
1. Remove Duplicates from Sorted List II
class Solution { public: ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) { ListNode* p = pHead1; int count1 = 0, count2 = 0; while (p != NULL) { count1++; p = p->next; } p = pHead2; while (p != NULL) { count2++; p = p->next; } if (count1 < count2) { return findNode(pHead1, count1, pHead2, count2); } else { return findNode(pHead2, count2, pHead1, count1); } } ListNode* findNode(ListNode* pHead1, int count1, ListNode* pHead2, int count2) { if (pHead1 == NULL) { return NULL; } int tmp = count2 - count1; ListNode* p2 = pHead2, *p1 = pHead1; while (tmp--) { p2 = p2->next; } while (p1 != NULL && p1 != p2) { p1 = p1->next; p2 = p2->next; } return p1; } };
View Code
参考剑指offer P193. 本题说的“公共节点”不是指“值相等”,而是“同一个节点”。即两链表在该点处汇合。
相关文章推荐
- Swiper滑动Html5手机浏览器自适应
- HTML5 INPUT新增属性
- html5 完整图片上传
- html5之多媒体
- html5画四边形
- html5实现渐变效果
- Html5学习之表单提交
- Html5学习之Form
- HTML5画布(CANVAS)速查简表
- 如何在Ubuntu手机平台中开发Cordova HTML5应用
- HTML5 audio标签的使用
- (转)HTML5游戏如何挣钱?2条经验让你每款赚3万刀
- 关于html5不支持frameset的解决方法
- html5.0与html4的“区别”
- H5点击及focus效果
- Html5之web workers多线程
- html5中table操作
- h5推广页面心得
- Html5中的<section>标签
- 了解HTML5和“她”的 API (一)