[LeetCode160]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.
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.
思路:求一个单链表交集的首结点
方法一:找出两个链表的长度差n,长链表先走n步,然后同时移动,判断有没有相同结点
方法二:两个链表同时移动,链表到尾部后,跳到链表头部,相遇点即为所求
代码:
方法一:
- /**
- * 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) {
- //方法一
- ListNode *p = headA;
- ListNode *q = headB;
- if(!p || !q) return NULL;
- while(p && q && p != q)
- {
- p = p->next;
- q = q->next;
- if(p == q)
- return p;
- if(!p)
- p = headB;
- if(!q)
- q = headA;
- }
- return p;
- }
- };
方法二:
- /**
- * 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 == NULL || headB == NULL) return NULL;
- ListNode *p = headA;
- ListNode *q = headB;
- int countA = , countB = ;
- while (p->next)
- {
- p = p->next;
- ++countA;
- }
- while (q->next)
- {
- q = q->next;
- ++countB;
- }
- if (p != q) return NULL;
- if (countA > countB)
- {
- for (int i = ; i < countA - countB; ++i)
- headA = headA->next;
- }
- else if (countB > countA)
- {
- for (int i = ; i < countB - countA; ++i)
- headB = headB->next;
- }
- while (headA != headB)
- {
- headA = headA->next;
- headB = headB->next;
- }
- return headA;
- }
- };
[LeetCode160]Intersection of Two Linked Lists的更多相关文章
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [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 ...
随机推荐
- loj1236(数学)
传送门:Pairs Forming LCM 题意:题意:问符合 lcm(i,j)=n (1<=i<=j<=n,1<=n<=10^14) 的 (i,j) 有多少对. 分析: ...
- cocos2d-x CCNode类
文章引用自http://blog.csdn.net/qiurisuixiang/article/details/8763260 1 CCNode是cocos2d-x中一个非常重要的类.CCNode是场 ...
- Android 自己定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...
- Linux Server
Linux Server CentOS 6.3下配置iSCSI网络存储 摘要: 一.简介iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运 ...
- 全面剖析Redis Cluster原理和应用 (转)
1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最核心的目标有三个: 性能:这是Redis赖以生 ...
- 《SAS编程和数据挖掘商业案例》第14部分学习笔记
继续<SAS编程与数据挖掘商业案例>学习笔记系列,本次重点:经常使用全程语句 所谓全程语句.是指能够用在不论什么地方的sas语句,既能够用在data数据步语句里面,也能够用在proc过程步 ...
- java 调用mysql的存储过程(简单示例)
首先我在mysql的test数据库里定义了一个student表: create table student4( id int primary key, sanme char(5) ); 插入几 ...
- sql语句中 limi的用法
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset 使用查询语句时需要返回前几条或者中间的某几行数据时可以用到limit 例如 ...
- 纯CSS滑动效果
原文地址:Pure CSS Slide Up and Slide Down 示例地址:Pure CSS Slide Demo 原文日期: 2013年08月26日 翻译日期: 2013年08月27日 如 ...
- PHP 文件操作类(创建文件并写入) 生成日志
<?php /** * 文件操作(生成日志)支持多条插入 * (假设插入多条语句并换行 用','逗号分开) * */ class log { public $path = './info.txt ...