leetcode160. Intersection of Two Linked Lists
来源:互联网 发布:官方软件下载中心 编辑:程序博客网 时间:2024/06/05 14:58
本道题找到两条链表相交部分的第一个节点,如果没有则返回null。我的思路是,如果两条链表相交,则从它们第一个相同的节点开始后面的部分均相同。那么就找到一个size = min(size1,size2),两条链表在这个size范围内依次迭代直到找到第一个相同的节点为止。
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode p = headA, q = headB; int size1 = 1, size2 = 1; while (p.next != null) { size1++; p = p.next; } while (q.next != null) { size2++; q = q.next; } p = headA; q = headB; int min = (int)Math.min(size1, size2); for (int i = 0; i < size1 - min; i++) p = p.next; for (int i = 0; i < size2 - min; i++) q = q.next; while (q != p) { q = q.next; p = p.next; } return q != null ? q : null; }}
阅读全文
0 0
- [leetcode160链表]Intersection of Two Linked Lists
- Leetcode160-Intersection of Two Linked Lists
- [LeetCode160] Intersection of Two Linked Lists
- leetcode160---Intersection of Two Linked Lists(交叉点)
- LeetCode160. Intersection of Two Linked Lists
- 【Leetcode160】. Intersection of Two Linked Lists-题解
- LeetCode160. Intersection of Two Linked Lists
- leetcode160~Intersection of Two Linked Lists
- leetcode160. Intersection of Two Linked Lists
- leetcode160-Intersection of Two Linked Lists
- LeetCode160. Intersection of Two Linked Lists
- Leetcode160. Intersection of Two Linked Lists/C++
- LeetCode160——Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- HDU 5542 The Battle of Chibi
- 15个开源的顶级人工智能工具
- Js数据类型的学习
- MQTT C Client实现消息推送(入门指南)
- 用C#开发简易小游戏之五子棋
- leetcode160. Intersection of Two Linked Lists
- Java中HashMap详解
- 树状数组求逆序对
- Java处理文件上传时文件的中文名出现乱码问题
- iTween基础之功能简介
- java 创建索引、删除索引(background)
- 使用js做一个简单的秒表计时
- POJ 2411 HDU4804【轮廓线+状压DP】
- java JDBC基础流程