160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists【easy】
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.
解法一:
- class Solution {
- public:
- /**
- * @param headA: the first list
- * @param headB: the second list
- * @return: a ListNode
- */
- ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
- // write your code here
- if(headA == NULL || headB == NULL)
- return NULL;
- ListNode* iter1 = headA;
- ListNode* iter2 = headB;
- int len1 = ;
- while(iter1->next != NULL)
- {
- iter1 = iter1->next;
- len1 ++;
- }
- int len2 = ;
- while(iter2->next != NULL)
- {
- iter2 = iter2->next;
- len2 ++;
- }
- if(iter1 != iter2)
- return NULL;
- if(len1 > len2)
- {
- for(int i = ; i < len1-len2; i ++)
- headA = headA->next;
- }
- else if(len2 > len1)
- {
- for(int i = ; i < len2-len1; i ++)
- headB = headB->next;
- }
- while(headA != headB)
- {
- headA = headA->next;
- headB = headB->next;
- }
- return headA;
- }
- };
先算长度,然后长的先走差值步,然后同时走
解法二:
- public class Solution {
- /**
- * @param headA: the first list
- * @param headB: the second list
- * @return: a ListNode
- */
- public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
- if (headA == null || headB == null) {
- return null;
- }
- // get the tail of list A.
- ListNode node = headA;
- while (node.next != null) {
- node = node.next;
- }
- node.next = headB;
- ListNode result = listCycleII(headA);
- node.next = null;
- return result;
- }
- private ListNode listCycleII(ListNode head) {
- ListNode slow = head, fast = head.next;
- while (slow != fast) {
- if (fast == null || fast.next == null) {
- return null;
- }
- slow = slow.next;
- fast = fast.next.next;
- }
- slow = head;
- fast = fast.next;
- while (slow != fast) {
- slow = slow.next;
- fast = fast.next;
- }
- return slow;
- }
- }
先弄成环,转换为找环的入口问题,找到之后再断开环
找环的问题解法可以参见(142. Linked List Cycle II【easy】)
160. Intersection of Two Linked Lists【easy】的更多相关文章
- 160. 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 ...
- 380. Intersection of Two Linked Lists【medium】
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- [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 ...
- 【LeetCode】160. Intersection of Two Linked Lists
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
随机推荐
- APIO2017伪题解
题目质量还是比较高的,只是当时澳大利亚方面出了一点问题?最后造成了区分度非常迷的局面. 纵观三道题,T1是披着交互外衣的提答题,T2是披着交互外衣的传统题,T3是一道据说近年来APIO最水的一道传统题 ...
- 某道我xjb想的题
Function 时限:5s 空限:256M (都是单点) Discription 现在你有一个函数: inline int f(int x){ int tot=0,alr=0,now; while( ...
- POJ 1113 Wall(凸包)
[题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...
- css layout入门(转)
元素与盒 在HTML中常常使用的概念是元素,而在CSS中,布局的基本单位是盒,盒总是矩形的. 元素与盒并非一一对应的关系,一个元素可能生成多个盒,CSS规则中的伪元素也可能生成盒,display属性为 ...
- Saga的实现模式——进化(Saga implementation patterns – variations)
在之前的几个博客中,我主要讲了两个saga的实现模式: 基于command的控制者模式 基于事件的观察者模式 当然,这些都不是实现saga的唯一方式.我们甚至可以将这些结合起来. 发布者——收集者 回 ...
- 学习Microsoft SQL Server 2008技术内幕:T-SQL语法基础
第 2 章: 单表查询 use TSQLFundamentals2008; select * from Sales.orders; select empid, year(orderdate) as o ...
- oracle表空间操作语句
1.查看所有表空间及表空间大小: select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from dba_data_files group by ...
- 几种常用的json序列化和反序列化工具介绍
一.前言 Json序列化和反序列化工作中会时常用到,也是目前数据交互的常用格式,Rest风格的接口加上json格式的数据交互,真的是天作之合. 目前Json字符与Json对象的相互转换方式有很多,接下 ...
- linux sudo使用学习记录
sudo在linux中非常重要,它能够使普通的用户临时拥有root权限.但是如果让用户滥用sudo命令的话可能会造成严重的影响. 例如:修改root的密码,切换到root用户等等. 所以我们虽然需要赋 ...
- JavaScript面向对象总结
对象(Object)应该算是js中最为重要的部分,也是js中非常难懂晦涩的一部分.更是面试以及框架设计中各出没.本文章,主要参考JavaScript红宝书(JavaScript高级程序设计 第六章)以 ...