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.

题意:

  给两个可能相交的链表,求出其交点位置。

思路:

  1.将其中一条链表(B)的首位相连,问题转换为“求一个带环链表(A)的入环位置”。

  2.从链表(A)起始处利用快慢指针(p1、p2)遍历环,得到快慢指针相等的结点(p1 == p2)的位置。

  3.将p1指向链表(A)的起始处后,p1、p2同步走。

  4.走至 p1 == p2 的位置处,即为所求结点。

  ps:1.要判断可能不相交的情况 2.不能改动原本的数据结构(B的尾指针在返回前要置为0)

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 ; ListNode *phb = headB;
while(phb->next != )
phb = phb->next;
phb->next = headB; bool isloop = false;
ListNode *pha = headA;
while(pha != )
{
if(pha == headB)
{
isloop = true;
break;
}
pha = pha->next;
} if(!isloop)
{
phb->next = ;
return ;
} ListNode *p1 = headA->next;
ListNode *p2 = headA->next->next; while(p1 != p2)
{
p1 = p1->next;
p2 = p2->next->next;
} p1 = headA; while(p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
} phb->next = ;
return p1;
}
};

【LeetCode 160】Intersection of Two Linked Lists的更多相关文章

  1. (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 ...

  2. [LeetCode 题解]:Intersection of Two Linked Lists

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Suppose an ...

  3. 【leetcode】Intersection of Two Linked Lists

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

  4. 【leetcode】Intersection of Two Linked Lists(easy)

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

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

  6. LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

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

  7. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  8. 【leetcode❤python】Intersection of Two Arrays

    #-*- coding: UTF-8 -*- #求两个集合的交集class Solution(object):    def intersection(self, nums1, nums2):     ...

  9. LeetCode算法题-Intersection of Two Linked Lists(Java实现)

    这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...

随机推荐

  1. Android 手机震动 设置震动时间

    开启震动,单次,5秒: Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); //震动5秒 vibrator.vibra ...

  2. 深入浅出Java并发包—指令重排序

    前面大致提到了JDK中的一些个原子类,也提到原子类是并发的基础,更提到所谓的线程安全,其实这些类或者并发包中的这么一些类,都是为了保证系统在运行时是线程安全的,那到底怎么样才算是线程安全呢? Java ...

  3. iOS开发--计时器-NSTimer与CADisplayLink

    如果程序要让某个方法重复执行,可以借助定时器来完成.CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器,NSTimer的精确度低了点,比如NSTimer的触发时间 ...

  4. ADO.NET入门教程(二)了解.NET数据提供程序

    出处:http://www.cnblogs.com/liuhaorain/archive/2012/02/11/2346312.html 1. 什么是.NET数据提供程序? .NET Framewor ...

  5. PowerDesigner连接Oracle数据库建表序列号实现自动增长

    原文:PowerDesigner连接Oracle数据库建表序列号实现自动增长 创建表就不说了.下面开始介绍设置自动增长列. 1 在表视图的列上创建.双击表视图,打开table properties — ...

  6. Android编译系统详解(一)

    ++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...

  7. hive 学习笔记——表的入门操作和命令

    1.受控表(managed table)包括内部表.分区表.桶表: 1.1.分区表 创建分区表: create table banji(id INT,name STRING) partitioned ...

  8. struts1,struts2,springMVC终极对比

    最近做项目用到了struts2,之前一直是用struts1和springMVC.感觉到了struts2从很大程度上和这两个还是有很大区别的,所以今天搜集了些资料,给他们做一下对比. Struts1官方 ...

  9. 将SQLServer2005中的数据同步到Oracle中

    有时由于项目开发的需要,必须将SQLServer2005中的某些表同步到Oracle数据库中,由其他其他系统来读取这些数据.不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. ...

  10. cmd打开git

    一旦你的git安装成功,而且在安装是没有选择可以使用cmd黑窗口来打开,那么你再来设置会很麻烦,我表示不会. 今天分享下同事分享给我的一个不错的方法. 这个方法依赖一个 Dos 文件(.bat文件), ...