LeetCode 160. 相交链表(Intersection of Two Linked Lists)
题目描述
编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
在节点 c1 开始相交。
注意:
- 如果两个链表没有交点,返回
null. - 在返回结果后,两个链表仍须保持原有的结构。
- 可假定整个链表结构中没有循环。
- 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
解题思路
首先分别获得两个链表的长度,然后让较长的链表先走两个长度的差值,使得两链表尾部对齐。然后两个链表再同时向后移动,并比较节点是否相等。
代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getLength(ListNode *head){
int len = ;
ListNode *node = head;
while(node){
len++;
node = node->next;
}
return len;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = getLength(headA), lenB = getLength(headB);
if(lenA < lenB) return getIntersectionNode(headB, headA);
int p = lenA - lenB;
ListNode *nodeA = headA, *nodeB = headB;
while(p){
nodeA = nodeA->next;
p--;
}
while(nodeA){
if(nodeA == nodeB) break;
nodeA = nodeA->next;
nodeB = nodeB->next;
}
return nodeA;
}
};
LeetCode 160. 相交链表(Intersection of Two Linked Lists)的更多相关文章
- LeetCode 160: 相交链表 Intersection of Two Linked Lists
爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Java实现 LeetCode 160 相交链表
160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...
- LeetCode 160——相交链表(JAVA)
编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- LeetCode 160 相交链表
题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], li ...
- leetcode 160相交链表
暴力解法当然可以遍历两个链表,不过time O(mn) space O(1)暂且不说, 方法一:双指针, time O(m+n),space O(1) 可以对比判断环形链表的快慢指针法. 这种方法构思 ...
- LeetCode 160. 相交链表 (找出两个链表的公共结点)
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点. 如下面的两 ...
- [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 ...
- [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 ...
随机推荐
- mac 在finder上面显示完成路径
打开终端,输入以下命令并回车: defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 然后再把finder关了再打开,你会 ...
- QT Creator 使用SVN的版本号做为编译的版本信息
在使用qt Creator 开发中,如果想使用 svn 的源代码版本号来作为 build 的一个子版本号或者只是为了识别某个发布版本,与源代码的版本信息对应起来,可以方便调试对应的版本代码,我们可以通 ...
- DX使用随记--TabControl
1. 关闭TabControl选项卡: Private Sub TabControl_Main_CloseButtonClick(sender As Object, e As EventArgs) H ...
- 在线预览word、excel文件
直接使用微软提供的在线预览服务. 免费 文件必须为网可访问地址,因为微软的服务器需要访问该文件
- Django获取用户form表单
首先创建一个Django 的工程项目 前面我们说过了,那到一个项目首先把模板路径,和静态路径在settings.py设置好以后,在开始写代码,写代码也要按照我们以前说的那个工程目录结构写. 现在我们做 ...
- safari同步google书签
1 直接通过safari的导入书签,from chrome就可以了
- GOLANG的继承语法练习
package main import( "fmt" _"sort" _"math/rand" ) // type WuDangMaster ...
- Django模型层:单表操作,多表操作,常用(非常用)字段和参数,Django-model进阶
一.web应用 二.模板的导入与继承 三.静态文件相关 四.inclusion_tag:返回html片段 五.模型层 一.web应用 -s包括两个部分:web服务器+application -目前阶段 ...
- cursor:not-allowed
今天发现了一个鼠标样式:not-allowed,是一个红色圈加一个斜杠,表示禁止的意思,似乎IE ,chrome firefox 都可以正常显示,很好用
- oracle 初试 hint
最近在研究oracle的视图问题,本来想全转成 物化视图(materialized view)的,这样可以极大提升系统的响应时间,无奈工作量太大,所以就研究了SQL优化的问题. 我这个普通视图 有36 ...