Intersection of Two Linked Lists 判断两个单链表里边有没有重叠,返回重叠的第一个节点
来源:互联网 发布:restful 接口实例java 编辑:程序博客网 时间:2024/06/05 04:57
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.
Notes:
- If the two linked lists have no intersection at all, return
null
. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory./**
- 题目重叠的意思是后边的都得是相同的,光交叉相等一个是不行的(除了最后的节点),如果是单向链表,两个有公共结点而部分重合的链表,拓扑形状看起来像一个Y,而不可能像X。
- * 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) {
int a=0,b=0,sub,i=0; //必须要初始化,这儿不初始化,下边也得要赋值,不然报错
ListNode nodeA=headA,nodeB=headB; //看,可以定义在同一行
if(headA==null || headB==null) return null;
else{
while(nodeA.next!=null){
nodeA=nodeA.next;
++a;
}
while(nodeB.next!=null){
nodeB=nodeB.next;
++b;
}
}
sub=a>=b? a-b : b-a; //可以使用Math.abs(a-b);
nodeA=a>=b? headA : headB;
nodeB=a>=b? headB : headA;
while(i<sub){
nodeA=nodeA.next;
++i;
}
while(nodeA!=nodeB){ //不错哦,如果一旦相等就返回
nodeA=nodeA.next;
nodeB=nodeB.next;
}
return nodeA; //包含了相等于不相等两种情况
}
}
思路一样,写法稍微有点差别:http://www.1point3acres.com/bbs/thread-110377-1-1.html
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
int lenA = length(headA);
int lenB = length(headB);
int diff = Math.abs(lenA - lenB);
while(diff > 0) {
if (lenA > lenB) headA = headA.next;
else headB = headB.next;
diff--;
}
while (headA != null && headB != null) {
if (headA.val == headB.val) return headA;
headA = headA.next;
headB = headB.next;
}
return null;
}
private int length(ListNode n) {
if (n == null) return 0;
int length = 0;
while (n != null) {
length++;
n = n.next;
}
return length;
}
}
- Intersection of Two Linked Lists 判断两个单链表里边有没有重叠,返回重叠的第一个节点
- leetcode解题之160# Intersection of Two Linked Lists Java版 (两个链表的第一个公共结点)
- [leetcode]Intersection of Two Linked Lists[java]找出两个链表的公共节点
- Intersection of Two Linked Lists(有优化)
- LeetCode---(160)Intersection of Two Linked Lists判断两个链表是否相交
- 【leetcode 两个链表的交集点】Intersection of Two Linked Lists
- LeetCode OJ 之 Intersection of Two Linked Lists (两个链表的交点)
- 【leetcode 两个链表的交集点】Intersection of Two Linked Lists
- (LeetCode)Intersection of Two Linked Lists --- 两个链表的交集
- [Lintcode]Intersection of Two Linked Lists 两个链表的交叉
- 160. Intersection of Two Linked Lists (求两个链表的交集)
- 380.Intersection of Two Linked Lists-两个链表的交叉(中等题)
- 160 Intersection of Two Linked Lists 求两个链表的交点
- LeetCode--Intersection of Two Linked Lists (两个链表的交点)Python
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- android studio中如何设置注释模板
- C语言指针-----指针与文件
- Oracle中的事务
- Codeforces 607A Chain Reaction 【二分 + dp】
- Ubuntu 15.10创建桌面快捷方式、启动器
- Intersection of Two Linked Lists 判断两个单链表里边有没有重叠,返回重叠的第一个节点
- 在CentOS 6.6 64bit上安装vim智能补全插件YouCompleteMe
- JDBC部分回顾注意点
- 第九章(1)-事件处理机制-学习笔记
- sslsocket双向验证通信及单向验证通信
- Oracle中存储器(procedure)和触发器(trigger)的小例子
- android入门_采用HttpURLConnection的GET方式实现登陆案例
- C++对象在内存中的存储
- Linux下dpkg命令的使用