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.

Credits:

Special thanks to @stellari for adding this problem and creating all test cases.

算法:

1)把其中的一条链首尾相连,组成一个环,这样问题就转化成:判断一条链表是否有环,如果有,求环的入口问题。

2)把两个链表都首尾颠倒顺序,然后比较。比较完之后,再颠倒把链表的顺序,恢复原样。

算法1的代码:

c++

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(!headA||!headB)
return NULL;
ListNode *p = headB;
while(p->next){
p=p->next;
}
ListNode *tailB = p;
tailB->next=headB; ListNode *p1,*p2;
p1=headA;
p2=headA;
while(p1){
p1=p1->next;
p2=p2->next;
if(p1){
p1=p1->next;
}else{
tailB->next = NULL;
return NULL;
}
if(p1==p2){
break;
}
}
if(p1==NULL){
tailB->next = NULL;
return NULL;
}
p2=headA;
while(p1!=p2){
p2=p2->next;
p1=p1->next;
}
tailB->next = NULL;
return p2; }
};

Intersection of Two Linked Lists的更多相关文章

  1. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

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

  3. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  4. LeetCode: Intersection of Two Linked Lists 解题报告

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

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

  6. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  7. LeetCode_160. Intersection of Two Linked Lists

    160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...

  8. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  9. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  10. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

随机推荐

  1. 大熊君大话NodeJS之------基于Connect中间件的小应用(Bigbear记事本应用之第一篇)

    一,开篇分析 大家好哦,大熊君又来了,昨天因为有点个人的事没有写博客,今天又出来了一篇,这篇主要是写一个记事本的小应用,前面的文章, 我也介绍过“Connect”中间件的使用以及“Mongodb”的用 ...

  2. 【Bootstrap】Bootstrap-select多选下拉框实现

    目录 前言 需要引用的它们 核心选项 核心方法 实例应用 回到顶部 前言 项目中要实现多选,就想到用插件,选择了bootstrap-select. 附上官网api链接,http://silviomor ...

  3. UI第十六节——UITabBarController详解

    一.UITabBarController主要用来管理你提供的content view controllers,而每一个 content view controller则负责管理自己的view层级关系, ...

  4. shell--1.shell 相关及变量

    1.shell脚本解释器 Bourme Shell (/usr/bin/sh 或 /bin/sh ) Bourme Again Shell ( /bin/bash ) C Shell ( /usr/b ...

  5. 关于git的简单实用命令

    时代在进步啊,现在已经不是svn的时代了,好多人都在使用git.所以自己也稍微学习了下git的使用. 常见的通过git提交代码步骤: git status :查看文件状态 :该命令显示你工程内修改的所 ...

  6. java入门 第三季4

    java集合框架中 java集合框架下

  7. Python网络编程之线程,进程

    一. 线程: 基本使用 线程锁 线程池 队列(生产者消费者模型) 二. 进程:  基本使用  进程锁 进程池 进程数据共享 三. 协程: gevent greenlet 四. 缓存: memcache ...

  8. c语言libcurl库的异步用法

    multi接口的使用会比easy 接口稍微复杂点,毕竟multi接口是依赖easy接口的,首先粗略的讲下其使用流程:curl_multi _init初始化一个multi curl对象,为了同时进行多个 ...

  9. VB中复制-粘贴-剪切键实现

    If Me.ActiveControl.GetType.BaseType.ToString = "System.Windows.Forms.TextBoxBase" Then Wi ...

  10. C#调用webservice简单实例

    如何利用IIS创建webservice不多做阐述,直接讲C#代码中如何调用已创建好的webservice. 首先在VS2010中新建一个工程项目,然后右键点击工程名选择添加服务引用. 在URL一栏中输 ...