160. Intersection of Two Linked Lists
来源:互联网 发布:免费网络宣传 编辑:程序博客网 时间:2024/05/26 08:42
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3
begin to intersect at node c1.
/** * 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) { int lenA = 1, lenB = 1; ListNode* newHeadA = headA; ListNode* newHeadB = headB; while(headA != NULL && headA->next != NULL){ headA = headA->next; ++lenA; } while(headB != NULL && headB->next != NULL){ headB = headB->next; ++lenB; } if(headA != headB) return NULL; while(lenA > lenB){ newHeadA = newHeadA->next; --lenA; } while(lenB > lenA){ newHeadB = newHeadB->next; --lenB; } while(newHeadA != newHeadB){ newHeadA = newHeadA->next; newHeadB = newHeadB->next; } return newHeadA; }};
0 0
- [LeetCode]160.Intersection of Two Linked Lists
- LeetCode 160. Intersection of Two Linked Lists
- 【LeetCode】160.Intersection of Two Linked Lists
- [leetcode] 160.Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- [LeetCode]160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- LeetCode--160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- LeetCode 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 【LeetCode】160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- Android广播(BroadCast)
- struts2中上传与下载文件
- html5和CSS3的编码规范
- 汇编修改精灵注解之按键2 微调功能的使用方法:
- Effective Java 读书笔记——66:同步访问共享的可变数据
- 160. Intersection of Two Linked Lists
- 读《浪潮之巅》感想(2)
- Leetcode 39. Combination Sum
- python 6-3 如何解析简单的xml文档 使用标准库中的xml.etree.ElementTree,其中parse函数可以解析xml文档
- 机器学习基础 第五章 降维算法
- Linux Kernel 4.9中TCP BBR算法的科普解释
- 增强学习系列之(一):增强学习介绍
- 共同学习Java源代码-数据结构-TreeSet(一)
- 策略模式和Spring的结合在项目中的应用