zoukankan      html  css  js  c++  java
  • 寻找链表相交节点

    思路一:

    1、先求出两个链表ListA,ListB的长度LengthA,LengthB。

    2、然后先把长的链表头指针先往前走|LengthA - LengthB|步。

    3、最后两个链表头指针同步往前走一步,直到指向的对象相同。

    代码实现及测试用例:

    package com.qiusongde;
    
    public class Solution160 {
    
        public static void main(String[] args) {
            
            ListNode l1 = new ListNode(1);
            ListNode l2 = new ListNode(2);
            ListNode l3 = new ListNode(3);
            l1.next = l2;
            l2.next = l3;
            
            ListNode l4 = new ListNode(4);
            ListNode l5 = new ListNode(5);
            l4.next = l5;
            l5.next = l3;
            
            showListNode(l1);
            showListNode(l4);
            System.out.println(getIntersectionNode(l1, l4).val);
            
        }
        
        public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            
            if(headA == null || headB == null) {
                return null;
            }
            
            //compute the length of two list
            int sizeA = length(headA);
            int sizeB = length(headB);
            
            //counteract difference 
            while(sizeA > sizeB) {
                headA = headA.next;
                sizeA--;
            }
            while(sizeB > sizeA) {
                headB = headB.next;
                sizeB--;
            }
            
            //find the same node
            //if no intersection, the same node is null
            while(headA != headB) {
                headA = headA.next;
                headB = headB.next;
            }
            
            return headA;
            
        }
        
        private static int length(ListNode node) {
            int length = 0;
            
            while(node != null) {
                length++;
                node = node.next;
            }
            
            return length;
        }
        
        public static void showListNode(ListNode list) {
            
            if(list == null) {
                System.out.println(list);
                return;
            }
            
            while(list != null) {
                System.out.print(list.val + " ");
                list = list.next;
            }
            System.out.println();
            
        }
        
        public static class ListNode {
            int val;
            ListNode next;
            ListNode(int x) {
                val = x;
                next = null;
            }
        }
    
    }
    1 2 3 
    4 5 3 
    3

    思路2:

    不用求出链表长度的值,但其实算法复杂度是一样的。

    We can use two iterations to do that.

    In the first iteration, we will reset the pointer of one linkedlist to the head of another linkedlist after it reaches the tail node.

    In the second iteration, we will move two pointers until they points to the same node.

    Our operations in first iteration will help us counteract the difference.

    So if two linkedlist intersects, the meeting point in second iteration must be the intersection point.

    If the two linked lists have no intersection at all, then the meeting pointer in second iteration must be the tail node of both lists, which is null

    代码实现:

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //boundary check
        if(headA == null || headB == null) return null;
        
        ListNode a = headA;
        ListNode b = headB;
        
        //if a & b have different len, then we will stop the loop after second iteration
        while( a != b){
            //for the end of first iteration, we just reset the pointer to the head of another linkedlist
            a = a == null? headB : a.next;
            b = b == null? headA : b.next;    
        }
        
        return a;
    }

    参考:https://leetcode.com/problems/intersection-of-two-linked-lists/#/solutions

  • 相关阅读:
    lnmp一键安装包,安装多版本php,并开启redis与swoole
    wangEditor的使用
    记一次傻逼的录入
    PHP自动加载
    pip升级
    Ubuntu安装mycli,让mysql命令行可以自动提示
    Redis存储AccessToken
    微信小程序生成太阳码
    巧妙的新订单提醒功能
    使用pt-query-digest进行日志分析
  • 原文地址:https://www.cnblogs.com/songdechiu/p/6692470.html
Copyright © 2011-2022 走看看