【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 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的更多相关文章
- (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 ...
- [LeetCode 题解]:Intersection of Two Linked Lists
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- 【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 ...
- 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 ...
- 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 ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- 【leetcode❤python】Intersection of Two Arrays
#-*- coding: UTF-8 -*- #求两个集合的交集class Solution(object): def intersection(self, nums1, nums2): ...
- LeetCode算法题-Intersection of Two Linked Lists(Java实现)
这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...
随机推荐
- Linux Versus Windows, Ubuntu/Mint V XP/Vista/7
原文:http://petermoulding.com/linux_versus_windows_ubuntu_mint_v_xp_vista_7 Linux Versus Windows, Ubun ...
- 初识spring与quartz整合实现定时任务
参考资料: http://kevin19900306.iteye.com/blog/1397744 引用自别人的博客: 特别注意一点,与Spring3.1以下版本整合必须使用Quartz1,最初我拿2 ...
- Android中JSON数据格式的简单使用
源码: package com.wangzhu.demo; import java.io.BufferedReader; import java.io.IOException; import java ...
- winserver2008,运行可执行文件,提示 激活上下文生成失败。 找不到从属程序集 Microsoft.VC90.DebugCRT,processorArchitecture="x86"
首先我运行的可执行文件是在win7下使用vs2008生成的release版本的文件,为什么在运行的时候提示需要DebugCRT? 另外我在winserver2008上是安装了vc2008的运行环境的. ...
- java:定义线程
Thread是java.lang包的类,默认导入. 进程:操作系统中的程序,多进程即同时运行多个程序.线程:程序中的流,多线程即程序中有多个流同时执行. 一个线程用一个线程对象表示 创建线程的方法: ...
- Photoshop:制作金属质感-不锈钢纹理
效果图 过程: 1.滤镜->渲染->云彩 2.滤镜->模糊->高斯模糊 3.滤镜->杂色->添加杂色 ,数量10 4.滤镜->模糊->径向模糊 5.滤镜 ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- UVa 1301 - Fishnet
求出所有交点枚举每个四边形找最大面积即可. #include <cstdio> #include <cmath> #include <algorithm> usin ...
- CVS 文件自动移 tag 的 Python 脚本
CVS 文件自动移 tag 的 Python 脚本 背景 工作中使用的版本管理工具是 CVS,在两次发布中,如果修改的文件比较少,会选择用移 Tag 的方式来生成一个新 Tag 发布.文件比较少的情况 ...
- 在Windows 上的 Python
在 Windows 上, 安装 Python 有两种选择. ActiveState 制作了一个 Windows 上的 Python 安装程序称为 ActivePython, 它包含了一个完整的 Pyt ...