zoukankan      html  css  js  c++  java
  • Leetcode: 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.

    第一想法是用HashSet<ListNode>, A list先遍历,存HashSet,然后B list遍历,发现ListNode存在就返回。但是这个方法不满足O(1)memory的要求。

    再想了一会儿,略微受了点提醒,发现可以利用这个O(n) time做文章。这个条件方便我们scan list几次都可以。于是我想到了:

    先scan A list, 记录A list长度lenA, 再scan B list, 记录B list长度lenB. 看A list最后一个元素与 B list最后一个元素是否相同就可以知道是否intersect. 

    各自cursor回到各自list的开头,长的那个list的cursor先走|lenA - lenB|步,然后一起走,相遇的那一点就是所求

     1 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
     2     int lenA = length(headA), lenB = length(headB);
     3     // move headA and headB to the same start point
     4     while (lenA > lenB) {
     5         headA = headA.next;
     6         lenA--;
     7     }
     8     while (lenA < lenB) {
     9         headB = headB.next;
    10         lenB--;
    11     }
    12     // find the intersection until end
    13     while (headA != headB) {
    14         headA = headA.next;
    15         headB = headB.next;
    16     }
    17     return headA;
    18 }
    19 
    20 private int length(ListNode node) {
    21     int length = 0;
    22     while (node != null) {
    23         length++;
    24         node = node.next;
    25     }
    26     return length;
    27 }

    vote最高的做法:no need to calculate the difference in length. two pointers starts from both head. When one reach the end just set it at the head of another list. When two pointers join, that's the intersection

     1 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
     2     //boundary check
     3     if(headA == null || headB == null) return null;
     4     
     5     ListNode a = headA;
     6     ListNode b = headB;
     7     
     8     //if a & b have different len, then we will stop the loop after second iteration
     9     while( a != b){
    10         //for the end of first iteration, we just reset the pointer to the head of another linkedlist
    11         a = a == null? headB : a.next;
    12         b = b == null? headA : b.next;    
    13     }
    14     
    15     return a;
    16 }
  • 相关阅读:
    【cocos2d-x + Lua(2) C++和lua数据通讯之间的互调】
    【cocos2d-x + Lua(1) 绑定Lua并使用tolua++】
    【cocos2d-x 手游研发小技巧(6)聊天系统+字体高亮】
    【cocos2d-x 仙凡奇缘-网游研发(2) 角色换线系统】
    【cocos2d-x 仙凡奇缘-网游研发(1) 登录&注册】
    python数据分析---第04章 NumPy基础:数组和矢量计算
    python面向对象(一)
    python 常用模块之random,os,sys 模块
    (python数据分析)第03章 Python的数据结构、函数和文件
    python常用模块之时间模块
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/4179351.html
Copyright © 2011-2022 走看看