[leetcode] Intersection of Two Linked Lists
来源:互联网 发布:sql数据仓库培训 编辑:程序博客网 时间:2024/06/16 04:59
Intersection of Two Linked Lists
双指针,求长度,作差,第一个相等的节点即为最早的公共节点。
/** * 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) { ListNode *la=headA; ListNode *lb=headB; int sa=0,sb=0; while(la!=NULL){ la=la->next; sa++; } while(lb!=NULL){ lb=lb->next; sb++; } int dis=0; if(sa<=sb){ dis=sb-sa; la=headA;//point to headA lb=headB; while(dis--){ lb=lb->next; } }else{ dis=sa-sb; la=headA; lb=headB;//point to headB while(dis--){ la=la->next; } } while(la!=lb){ la=la->next; lb=lb->next; } return la; }};
0 0
- [leetcode] [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
- Intersection of Two Linked Lists--leetcode
- [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 Intersection of Two Linked Lists
- 黑马程序员----java单例设计模式
- PHP json 类型
- OJ【3083】 寻找helloworld
- 5分钟搞定内存字节对齐
- 正确理解oracle rac下的IP
- [leetcode] Intersection of Two Linked Lists
- PS 滤镜——波浪 wave
- 内存调试——valgrind工具对数组访问错误和内存泄漏的检测
- SQL Server中常用全局变量介绍
- 接口的作用(这里举C#的例子)
- 图像特效——摩尔纹 moir
- redolog files
- C++ 函数的重载
- 第二十章 线程局部存储区