Java实现-两个链表的交叉
2017-06-06 09:58
393 查看
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// Write your code here
if(headA==null||headB==null){
return null;
}
ListNode A=headA;
ListNode B=headB;
int A_length=1;
while(A.next!=null){
A=A.next;
A_length++;
}
int B_length=1;
while(B.next!=null){
B=B.next;
B_length++;
}
if(A!=B){
return null;
}
ListNode short_list=headA;
ListNode long_list=headB;
if(A_length>B_length){
long_list=headA;
short_list=headB;
}
for(int i=0;i<Math.abs(A_length-B_length);i++){
long_list=long_list.next;
}
while(long_list!=null){
if(long_list==short_list){
return long_list;
}else{
long_list=long_list.next;
short_list=short_list.next;
}
}
return null;
}
}
相关文章推荐
- JAVA实现合并两个排序的链表(《剑指offer》)
- 九度1519题 合并两个有序链表java实现,(也是剑指offer中的题)
- 剑指offer 面试题17 合并两个排序的链表-Java实现
- JAVA实现合并两个排序的链表(《剑指offer》)
- 两个链表的第一个公共结点java实现
- Java实现两个有序链表的合并
- 两个链表之间的联合----Java实现
- Java实现-交换链表当中的两个节点
- 两个链表的第一个公共节点(Java实现)
- 《剑指offer》编程题java实现(二十二):两个链表的第一个公共节点
- (整理)Java实现链表--找到两个链表的第一个公共结点(网易笔试题2016)
- 如何实现两个字符串的交叉合并功能java实现
- PAT:两个有序链表序列的合并(Java实现)
- Java实现-合并两个排序链表
- 剑指Offer--面试题17:合并两个排序的链表--Java实现
- 剑指offer----两个链表的第一个公共节点----java实现
- 数据结构与算法分析笔记与总结(java实现)--链表6:两个链表找公共结点问题
- 合并两个有序链表(Java递归实现)
- 剑指Offer 面试题25:合并两个排序的链表(递归+非递归) Java代码实现
- 合并两个排序的链表java实现