Java [Leetcode 160]Intersection of Two Linked Lists
题目描述:
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.
解题思路 :
主要是考虑两条链路不一样长的问题,那么在其中一条链路到底的时候立马换回到另一条链路上面,另一条链路到底的时候也换回到这一条链路上,这样保证了后面能够同时到达相交点。
代码如下:
/**
* 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 a = headA;
ListNode b = headB; while(a != b){
if(a == null)
a = headB;
else
a = a.next;
if(b == null)
b = headA;
else
b = b.next;
} return a;
}
}
Java [Leetcode 160]Intersection of Two Linked Lists的更多相关文章
- [LeetCode] 160. Intersection of Two Linked Lists 解题思路
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- Java for LeetCode 160 Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode 160. Intersection of Two Linked Lists (两个链表的交点)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Leetcode 160. Intersection of two linked lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- (链表 双指针) leetcode 160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode——160 Intersection of Two Linked Lists
题目 Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: ...
随机推荐
- linux源代码阅读笔记 find_entry分析
78 static struct buffer_head * find_entry(struct m_inode * dir, 79 const char * name, int namelen, s ...
- 【C++基础】sizeof 与 strlen的区别
要理解两者的区别,就要分别理解他们的本质 strlen(char *) 计算字符串的长度,内部实现是用一个循环计算字符串的长度,直到‘\0’为止 1.srtlen 是一个函数,参数只能为char 或者 ...
- 编写高性能JavaScript【转】
英文链接:Writing Fast, Memory-Efficient JavaScript 很多JavaScript引擎,如Google的V8引擎(被Chrome和Node所用),是专门为需要快速执 ...
- SNAT
http://blog.chinaunix.net/uid-2628744-id-2454879.html
- 【转】Dr.com 5.20破解教程
Dr.com 5.20破解教程 方法一 1.首先下载相关工具 Process Explorer(大家可以自行百度 一般绿色汉化版就可以)右键选择以管理员权限运行process的主程序 然后运行drc ...
- PKUSC 模拟赛 day1 下午总结
下午到了机房之后又困又饿,还要被强行摁着看英文题,简直差评 第一题是NOIP模拟赛的原题,随便模拟就好啦 本人模拟功力太渣不小心打错了个变量,居然调了40多分钟QAQ #include<cstd ...
- MySQL登录报错"Access denied for user 'root'@'localhost' (using password: YES)"
最近登录MySQL时候总报错: # mysql -uroot -p Enter password: ERROR (): Access denied for user 'root'@'localhost ...
- C++:友元(非成员友元函数、成员友元函数、友元类)
3.8 友元:友元函数和友元类 友元函数 :既可以是不属于任何类的非成员函数,也可以是另一个类的成员函数,统称为友元函数.友元函数不是当前类的成员函数,而是独立于类的外部函数,但它可以访问该类所有的 ...
- CMake with Win&MinGW
今天一个下午都在做一件简直耻辱play的事情,论文没看,程序没写,玩了一个下午的编译器...心塞(逃... 言归正传,今天要讲在windows下,使用Cmake和MInGW. 1.g++ MinGW的 ...
- 【c++】中文设置
#include <iostream> #include <string> #include<locale> using namespace std; ; int ...