题目描述

编写一个程序,找到两个单链表相交的起始节点。

例如,下面的两个链表:

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)的更多相关文章

  1. LeetCode 160: 相交链表 Intersection of Two Linked Lists

    爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...

  2. [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 ...

  3. Java实现 LeetCode 160 相交链表

    160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...

  4. LeetCode 160——相交链表(JAVA)

    编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...

  5. LeetCode 160 相交链表

    题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], li ...

  6. leetcode 160相交链表

    暴力解法当然可以遍历两个链表,不过time O(mn) space O(1)暂且不说, 方法一:双指针, time O(m+n),space O(1) 可以对比判断环形链表的快慢指针法. 这种方法构思 ...

  7. LeetCode 160. 相交链表 (找出两个链表的公共结点)

    题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点. 如下面的两 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. caffe笔记

    1. 训练    cifar10 示例 ① cd caffe.1.0.0 ./data/cifar10/get_cifar10.sh    #获取图片 ② ./examples/cifar10/cre ...

  2. scala中ClassOf、asInstenceOf、isInstanceOf三个预定义方法分析

    classOf.isInstanceOf.asInstanceOf三个预定义方法分析 Scala的三个预定义(predefined)方法,我们经常用到:它们用来感觉很简单, 但是里面还是隐藏了一些细节 ...

  3. github 提交和更新代码

    …or create a new repository on the command line   echo "# flutterPluginsWorks" >> RE ...

  4. 9.SpringMVC注解式开发-处理器的请求映射规则的定义

    1.对请求URI的命名空间的定义 @RequestMapping的value属性用于定义所匹配请求的URI.但对于注解在方法上和注解在类上, 其value 属性 所指定的URI,意义是不同的 一个@C ...

  5. 【python+beautifulsoup4】Beautifulsoup4

    Beautiful soup将复杂HTML文档转换成一个复杂的属性结构,每个节点都是python对象,所有对象可归纳为4种Tag,NavigableString,BeautifulSoup,Comme ...

  6. Redis汇总

    开源项目 https://www.cnblogs.com/yswenli/p/9460527.html

  7. java lambda 所有列求和

    今天做东西的时候遇到一个需求,求list集合所有列的求和.折腾半天也没有搞出来,网上大部分都是单列求和就像下面这样的,其他都差多,什么 min,max avg count 只得到了number这个属性 ...

  8. linux——系统命令

    (1)   显示系统日期和时间:date 显示系统当前时间 例如:date (1)  切换用户:su 用户名 以其他用户身份使用系统,(类似windows10系统,有些程序以管理员身份执行) ① 从r ...

  9. 7.caffe:create_lmdb.sh(数据预处理转换成lmdb格式)

    个人实践代码如下: #!/usr/bin/env sh # Create the imagenet lmdb inputs # N.B. set the path to the imagenet tr ...

  10. javascript 常用的一些原生方法

    一丶javascript------ reduce() reduce()方法: arr.reduce(function(prev,cur,index,arr){ ... }, init); 参数解释: ...