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:

  1. A: a1 a2

  2. c1 c2 c3

  3. 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.

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param headA: the first list
  5. * @param headB: the second list
  6. * @return: a ListNode
  7. */
  8. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  9. // write your code here
  10. if(headA == NULL || headB == NULL)
  11. return NULL;
  12. ListNode* iter1 = headA;
  13. ListNode* iter2 = headB;
  14. int len1 = ;
  15. while(iter1->next != NULL)
  16. {
  17. iter1 = iter1->next;
  18. len1 ++;
  19. }
  20. int len2 = ;
  21. while(iter2->next != NULL)
  22. {
  23. iter2 = iter2->next;
  24. len2 ++;
  25. }
  26. if(iter1 != iter2)
  27. return NULL;
  28. if(len1 > len2)
  29. {
  30. for(int i = ; i < len1-len2; i ++)
  31. headA = headA->next;
  32. }
  33. else if(len2 > len1)
  34. {
  35. for(int i = ; i < len2-len1; i ++)
  36. headB = headB->next;
  37. }
  38. while(headA != headB)
  39. {
  40. headA = headA->next;
  41. headB = headB->next;
  42. }
  43. return headA;
  44. }
  45. };

先算长度,然后长的先走差值步,然后同时走

解法二:

  1. public class Solution {
  2. /**
  3. * @param headA: the first list
  4. * @param headB: the second list
  5. * @return: a ListNode
  6. */
  7. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  8. if (headA == null || headB == null) {
  9. return null;
  10. }
  11.  
  12. // get the tail of list A.
  13. ListNode node = headA;
  14. while (node.next != null) {
  15. node = node.next;
  16. }
  17. node.next = headB;
  18. ListNode result = listCycleII(headA);
  19. node.next = null;
  20. return result;
  21. }
  22.  
  23. private ListNode listCycleII(ListNode head) {
  24. ListNode slow = head, fast = head.next;
  25.  
  26. while (slow != fast) {
  27. if (fast == null || fast.next == null) {
  28. return null;
  29. }
  30.  
  31. slow = slow.next;
  32. fast = fast.next.next;
  33. }
  34.  
  35. slow = head;
  36. fast = fast.next;
  37. while (slow != fast) {
  38. slow = slow.next;
  39. fast = fast.next;
  40. }
  41.  
  42. return slow;
  43. }
  44. }

先弄成环,转换为找环的入口问题,找到之后再断开环

找环的问题解法可以参见(142. Linked List Cycle II【easy】

160. Intersection of Two Linked Lists【easy】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  5. 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 ...

  6. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  7. [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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. APIO2017伪题解

    题目质量还是比较高的,只是当时澳大利亚方面出了一点问题?最后造成了区分度非常迷的局面. 纵观三道题,T1是披着交互外衣的提答题,T2是披着交互外衣的传统题,T3是一道据说近年来APIO最水的一道传统题 ...

  2. 某道我xjb想的题

    Function 时限:5s 空限:256M (都是单点) Discription 现在你有一个函数: inline int f(int x){ int tot=0,alr=0,now; while( ...

  3. POJ 1113 Wall(凸包)

    [题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...

  4. css layout入门(转)

    元素与盒 在HTML中常常使用的概念是元素,而在CSS中,布局的基本单位是盒,盒总是矩形的. 元素与盒并非一一对应的关系,一个元素可能生成多个盒,CSS规则中的伪元素也可能生成盒,display属性为 ...

  5. Saga的实现模式——进化(Saga implementation patterns – variations)

    在之前的几个博客中,我主要讲了两个saga的实现模式: 基于command的控制者模式 基于事件的观察者模式 当然,这些都不是实现saga的唯一方式.我们甚至可以将这些结合起来. 发布者——收集者 回 ...

  6. 学习Microsoft SQL Server 2008技术内幕:T-SQL语法基础

    第 2 章: 单表查询 use TSQLFundamentals2008; select * from Sales.orders; select empid, year(orderdate) as o ...

  7. oracle表空间操作语句

    1.查看所有表空间及表空间大小: select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from dba_data_files group by ...

  8. 几种常用的json序列化和反序列化工具介绍

    一.前言 Json序列化和反序列化工作中会时常用到,也是目前数据交互的常用格式,Rest风格的接口加上json格式的数据交互,真的是天作之合. 目前Json字符与Json对象的相互转换方式有很多,接下 ...

  9. linux sudo使用学习记录

    sudo在linux中非常重要,它能够使普通的用户临时拥有root权限.但是如果让用户滥用sudo命令的话可能会造成严重的影响. 例如:修改root的密码,切换到root用户等等. 所以我们虽然需要赋 ...

  10. JavaScript面向对象总结

    对象(Object)应该算是js中最为重要的部分,也是js中非常难懂晦涩的一部分.更是面试以及框架设计中各出没.本文章,主要参考JavaScript红宝书(JavaScript高级程序设计 第六章)以 ...