LeetCode Intersection of Two Linked Lists (找交叉点)
题意:
给两个链表,他们的后部分可能有公共点。请返回第一个公共节点的地址?若不重叠就返回null。
思路:
用时间O(n)和空间O(1)的做法。此题数据弱有些弱。
方法(1)假设两个链表A和B,用两个指针分别按顺序遍历AB和BA,这AB和BA肯定等长的。如果他们有公共点,那么按照这样走必定会在某个点相遇。所以只要预先判断是否有公共点,这只需要用两个指针分别指向A和B的链尾判断是否相同即可(这一步可以使用更简单的方法,增加1个计数器,判断指针1是否已经遍历过AB了)。
/**
* 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 *L1=headA;
ListNode *L2=headB;
//若无交点,且AB等长,那么他们会在null处相遇,退出。
//先判断是否有交点先。
while(L1&&L1->next) L1=L1->next;
while(L2&&L2->next) L2=L2->next;
if(L1!=L2) return NULL;
L1=headA;
L2=headB;
while(L1!=L2)
{
if(L1) L1=L1->next;
else L1=headB; if(L2) L2=L2->next;
else L2=headA;
}
return L1;
}
};
AC代码
方法(2)先统计链A和B的长度,假设A长,那么指针1先走|A|-|B|步,然后再同时走,若有公共点必定会相遇。
LeetCode Intersection of Two Linked Lists (找交叉点)的更多相关文章
- 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 Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- [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 ...
- LeetCode——Intersection of Two Linked Lists
Description: Write a program to find the node at which the intersection of two singly linked lists b ...
- [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 ...
- [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]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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
随机推荐
- C语言数据结构之栈:中缀表达式的计算
*注:本人技术不咋的,就是拿代码出来和大家看看,代码漏洞百出,完全没有优化,主要看气质,是吧 学了数据结构——栈,当然少不了习题.习题中最难的也是最有意思的就是这个中缀表达式的计算了(可以算+-*/和 ...
- Device disconnected
问题:android 调试的时候,Logcat没有任何输出,提示Device disconnected 解决:Devices -- Reset adb
- InnoDB外键使用小结
USE `wfc_database`; # 主表(也可以称作:被参照表.referenced table.outTable) ALTER TABLE `app` ENGINE=INNODB; # 从表 ...
- ViewData,ViewBag和TempData
ViewData ViewBag TempData 类型 字典 Dynamic TempDataDictionary 出生时间 MVC1 MVC3 框架版本 .net3.5 .net4.0 ...
- mir9-lua——《热血沙城》45度ARPG手游-Lua移植版
mir9——<热血沙城>,是9秒论坛开源的一个使用Cocos2d-x-2.2.1引擎开发的45度ARPG手游Demo,源代码为c++.mir9-lua是mir9的Lua移植版,使用Quic ...
- 1874: [BeiJing2009 WinterCamp]取石子游戏 - BZOJ
Description小H和小Z正在玩一个取石子游戏. 取石子游戏的规则是这样的,每个人每次可以从一堆石子中取出若干个石子,每次取石子的个数有限制,谁不能取石子时就会输掉游戏. 小H先进行操作,他想问 ...
- 1047: [HAOI2007]理想的正方形 - BZOJ
Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小.Input 第一行为3个整数,分别表示a,b,n的值第二行至第a ...
- java.lang.NoSuchMethodError: javaxservlet.http.HttpServletRequest.isAsyncStarted()Z
鸣谢网址:http://stackoverflow.com/questions/25940571/java-lang-nosuchmethoderror-javaxservlet-http-https ...
- leetcode4 Valid Palindrome回文数
Valid Palindrome回文数 whowhoha@outlook.com Question: Given a string, determine if it is a palindrome, ...
- uploadify 下载组件使用技巧和在线预览 word,excel,ppt,pdf的方案
http://www.cnblogs.com/wolf-sun/p/3565184.html uploadify 上传工具的使用技巧 http://www.cnblogs.com/wolf-sun/p ...