寻找链表相交节点

本文介绍两种寻找两链表交点的方法。一种是通过计算两链表长度差,让较长链表先移动一定步数;另一种是使用两次迭代,当达到尾节点时将指针重置到另一链表头部,最终相遇点即为交点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路一:

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

转载于:https://www.cnblogs.com/songdechiu/p/6692470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值