【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).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...
随机推荐
- Oracle 学习笔记(三)
1.插入有日期的表,使用 to_date 函数 to_date('1992-12-07', 'yyyy-mm-dd'); 2.使用update更新语句的时候,既可以使用表达式或者数值直接修改数据,也可 ...
- CAD导入ArcScene中线被打断 求解决方案
cad中是这样 但在arcscene里中是这样
- WP之Sql Server CE数据库
如何在WP8中进行数据存储,你首先想到应该是独立存储,但是独立存储似乎存储文件更方便,如果我们希望像处理对象的形式,该怎么办呢,答案就是Sql Server CE. Sql Server CE并不是新 ...
- wxpython ItemContainer
ItemContainer 是 很多可以添加string item的部件的父类,封装很多有用的方法,可以用来获取部件的被选中item 的string 如wx.ListBox ,wx.CheckList ...
- RestTemplateIntegrationTests
摘录RestTemplate的集成测试类/* 2. * Copyright 2002-2010 the original author or authors. 3. * 4. * L ...
- textbox不支持Ctrl+A
http://stackoverflow.com/questions/5885739/why-are-some-textboxes-not-accepting-control-a-shortcut-t ...
- [POJ2828]Buy Tickets(线段树,单点更新,二分,逆序)
题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位 ...
- C# MySQL 数据库操作类
using System; using System.Configuration; using System.Collections; using System.Data; using MySql.D ...
- CocoaPods requires your terminal to be using UTF-8 encoding
WARNING: CocoaPods requires your terminal to be using UTF-8 encoding. See https://github.com/CocoaPo ...
- Android wakelock机制
Wake Lock是一种锁的机制, 只要有人拿着这个锁,系统就无法进入休眠,可以被用户态程序和内核获得. 这个锁可以是有超时的或者是没有超时的,超时的锁会在时间过去以后自动解锁. 如果没有锁了或者 ...