题目描述

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

例如,下面的两个链表:

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. 逗渣的学习笔记-关于webpack从头撸一遍

    刚开始接触webpack,完全是工作需求.那是去年年末的事情了,当时被迫换到另一个项目组,也是一个新的项目,做手机上面的应用,客户要求用react做应用,所以完全属于赶鸭子上架,当时说真的蛮懵逼的,也 ...

  2. linux上网时断时续问题

    [问题描述]打开百度比较慢:登录远程服务器操作一会儿就断了 [问题环境]ip自动获取 [问题分析]自己的ip地址被人占用了 [问题解决]1)重新手动配置一个新的ip    2)释放原来的ip,重新自动 ...

  3. 使用Response下载(支持任何格式)

    使用Response下载 下面代码: protected void Button2_Click(object sender, EventArgs e) { Response.ContentType = ...

  4. linux——实际工作中如何使用linux

    实际工作中,linux系统都不会在我们自己的电脑上,linux系统安装在机房的服务器上,我们操作linux不可能跑到机房去,所以我们需要有一个工具,能在公司通过网络远程连接到机房的linux服务器上 ...

  5. mysql存储json

    1. json_merge 合并Json并返回 update `user` set inviteeMap = json_merge(inviteeMap, '{"xx1":100} ...

  6. Angular Multiple HTTP Requests with RxJS

    原文:https://coryrylan.com/blog/angular-multiple-http-requests-with-rxjs ----------------------------- ...

  7. LINUX查看内存使用情况 free

    # free 显示结果如下: Mem:表示物理内存统计 total 内存总数 8057964KB used 已使用的内存 7852484KB free 空闲的内存数 205480KB shared 当 ...

  8. 《深入理解Java虚拟机》之(一、内存区域)

    一.java的体系构成: Java的技术体系主要由支撑java程序运行的虚拟机.提供各种开发领域接口支持的java api.java编程语言及许多第三方java框架(如Spring .Struts等) ...

  9. 队列BlockingQueue的简单例子

    队列,当进行多线程编程的时候,很多时候可能会用到,队列是先进先出的,我们可以将要执行的任务放置在队列内缓存起来,当线程池中线程可以使用的时候,我们就从队列中获取一个任务执行.. 当前是一个队列的简单例 ...

  10. [React] Create a Persistent Reference to a Value Using React useRef Hook

    The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...