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

    begin to intersect at node c1.

     

    Example 1:

    Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
    Output: Reference of the node with value = 8
    Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

    注意:交点指的是同一个对象(PointA == PointB)!不是两个Node值相同 (PointA.val == PointB.val)

    Example 2:

    Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
    Output: Reference of the node with value = 2
    Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
    

     

    Example 3:

    Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
    Output: null
    Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
    Explanation: The two lists do not intersect, so return null.
    

     

    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.

    法一:

    如果两个链长度相同,那么同时向后移动指针,对应的一个个比下去就能找到,所以只需要把较长的链表变短即可,使得两个链表长度相同:

    定义两个指针,分别指向A,B链表头;

    分别遍历链表A, B, 得到A,B的长度,计算长度差值diff;

    把两个指针移回链表头部,把较长的链表的指针,先从头向后移动diff;

    两个指针,一一比较,每次同时向后移动一步。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            int alength = 0;
            int blength = 0;
            
         // 链表A B长度 ListNode aNode
    = headA; ListNode bNode = headB; while(aNode != null) { alength++; aNode = aNode.next; } while(bNode != null) { blength++; bNode = bNode.next; }
         // 将指针移回头结点 aNode
    = headA; bNode = headB;
         // 移动较长链表的指针diff步
    int diff = alength - blength; if(diff >= 0) { while(diff != 0) { diff--; aNode = aNode.next; } } else { diff = -1 * diff; while(diff != 0) { diff--; bNode = bNode.next; } }
        // find intersection
    while(aNode != bNode) { aNode = aNode.next; bNode = bNode.next; } return aNode; } }

     

     

    法二:

    用环的思想来做,我们让两条链表分别从各自的开头开始往后遍历,当其中一条遍历到末尾时,我们跳到另一个条链表的开头继续遍历。两个指针最终会相等,而且只有两种情况,一种情况是在交点处相遇,另一种情况是在各自的末尾的空节点处相等。为什么一定会相等呢,因为两个指针走过的路程相同,是两个链表的长度之和,所以一定会相等。

    代码很简洁 

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode aPointer = headA;
            ListNode bPointer = headB;
            
         // Intersection 条件: aPointer == bPointer, same address! Not aPointer.val == bPointer.val (see example 1)
    while(aPointer != bPointer) { aPointer = (aPointer != null) ? aPointer.next : headB; bPointer = (bPointer != null) ? bPointer.next : headA; } return aPointer; } }

     

  • 相关阅读:
    生成对抗性网络GAN
    一些程序员好用的网站
    TED演讲积累。
    JQuery$.extend()用法
    jQuery中判断数组
    input标签中的accpet
    gitlab的添加密钥
    Linux—Ubuntu14.0.5 修改gitlab管理员的密码
    Linux—Ubuntu14.0.5安装gitlab
    Linux—Ubuntu14.0.5安装Redis
  • 原文地址:https://www.cnblogs.com/Jessiezyr/p/12954432.html
Copyright © 2011-2022 走看看