Intersection of Two Linked Lists
来源:互联网 发布:公有云计算基础架构 编辑:程序博客网 时间:2024/05/14 08:49
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) { return null; } ListNode cur1 = headA; ListNode cur2 = headB; int len1 = 1, len2 = 1; while (cur1.next != null) { cur1 = cur1.next; len1++; } while (cur2.next != null) { cur2 = cur2.next; len2++; } cur1 = headA; cur2 = headB; if (len1 > len2) { for (int i = 0; i < len1 - len2; i++) { cur1 = cur1.next; } } else { for (int i = 0; i < len2 - len1; i++) { cur2 = cur2.next; } } while (cur1 != null && cur2 != null) { if (cur1 == cur2) { return cur1; } cur1 = cur1.next; cur2 = cur2.next; } return null; }}
0 0
- 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
- 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
- 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
- Android之activity与application生命周期
- Android 代码混淆
- 正则表达式
- 高德地图#import <MAMapKit/MAMapKit.h> file not found的报错解决办法
- 删除.SVN+批量删除/增加文件
- Intersection of Two Linked Lists
- 创业项目开发阶段性总结
- error: No resource identifier found for attribute 'format24Hour' in package 'android'
- [LeetCode][Java] Longest Common Prefix
- JS原生函数总结
- linux shell中 if else以及大于、小于、等于逻辑表达式介绍
- Linked List Cycle II
- FreeMarker语法知识
- Java基础日记———IO流(下)