LeetCode题解:Intersection of Two Linked Lists
来源:互联网 发布:数据分享网站 编辑:程序博客网 时间:2024/06/09 02:25
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.
题意:找到两个链表的交点
解决思路:两个链表在同一个起点开始向下找
代码:
public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { int lenA = length(headA); int lenB = length(headB); while(lenA > lenB){ headA = headA.next; --lenA; } while(lenA < lenB){ headB = headB.next; --lenB; } while(headA != headB){ headA = headA.next; headB = headB.next; } return headA; } public int length(ListNode head){ int length = 0; ListNode node = head; while(node != null){ ++length; node = node.next; } return length; }}
0 0
- LeetCode题解:Intersection of Two Linked Lists
- [LeetCode] Intersection of Two Linked Lists 题解
- LeetCode 题解(216) : Intersection of Two Linked Lists
- LeetCode题解——Intersection of Two Linked Lists
- Leetcode题解 160. Intersection of Two Linked Lists
- leetcode题解-160. Intersection of Two Linked Lists
- 【Leetcode160】. 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
- Intersection of Two Linked Lists--leetcode
- [leetcode]Intersection of Two Linked Lists
- leetcode:Intersection of Two Linked Lists
- Activity之间信息传递(参数传递)
- Android内存泄露检测利器---LeakCanary
- Python游戏服务器开发日记(四)scons编译工具、C和C++混合使用
- 网络方面的面试题
- HDU-2546 饭卡
- LeetCode题解:Intersection of Two Linked Lists
- C++实现观察者(Observer)模式
- 最短路径
- codecombat之边远地区的森林23-30关及地牢40\41关代码分享
- MFC 绘制半透明图片
- 屌丝giser成长记-大学篇
- Activity 主界面服务
- 逻辑运算符及循环语句
- Dotcms中文教程之一:管理(1、配置介绍)