【LeetCode 160】相交链表

相交链表

方法一

刚看题目没有头绪,想到之前写的链表倒数第k个节点,首先遍历链表,找到链表长度,通过两个链表长度的差使链表末端对齐。通过对末端对齐后的两链表同时遍历,对比下一节点是否相同判断结果。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
       ListNode a = headA;
       ListNode b = headB;
       ListNode A = new ListNode();
       ListNode B = new ListNode();
       A.next = headA;
       B.next = headB; 
       int numa = 0, numb = 0;
       int len = 0;
       while(a != null){
           numa++;
           a = a.next;
       }   
       while(b != null){
           numb++;
           b = b.next;
       }
       len = numa - numb;
       if(len > 0){
           while(len > 0){
               A = A.next;
               len--;
           }
           while(A != null && B != null){
               if(A.next == B.next) return A.next;
               A = A.next;
               B = B.next;
           }
           return null;
       }else{
           len = -len;
           while(len > 0){
               B = B.next;
               len--; 
           }
           while(A != null && B != null){
               if(A.next == B.next) return A.next;
               A = A.next;
               B = B.next;
           }
           return null;
       } 
    }
}

方法二

先便利自身的链表,再遍历另一个链表
原因如下:
若有公共部分
公共部分的长度为c,链表一长度为a,链表b长度为b
则两个指针同时指到公共节点需要走过的长度为
a + (b - c)和 b + (a - c)
到公共部分就会停止,返回a或b都可

在这里插入图片描述
若没有公共部分,此时为图所示,最后所指的都为null,与题意相符,返回null即可。
在这里插入图片描述

/**
 * 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 a = headA;
    //    ListNode b = headB;
    //    ListNode A = new ListNode();
    //    ListNode B = new ListNode();
    //    A.next = headA;
    //    B.next = headB; 
    //    int numa = 0, numb = 0;
    //    int len = 0;
    //    while(a != null){
    //        numa++;
    //        a = a.next;
    //    }   
    //    while(b != null){
    //        numb++;
    //        b = b.next;
    //    }
    //    len = numa - numb;
    //    if(len > 0){
    //        while(len > 0){
    //            A = A.next;
    //            len--;
    //        }
    //        while(A != null && B != null){
    //            if(A.next == B.next) return A.next;
    //            A = A.next;
    //            B = B.next;
    //        }
    //        return null;
    //    }else{
    //        len = -len;
    //        while(len > 0){
    //            B = B.next;
    //            len--; 
    //        }
    //        while(A != null && B != null){
    //            if(A.next == B.next) return A.next;
    //            A = A.next;
    //            B = B.next;
    //        }
    //        return null;
    //    } 
    ListNode a = headA;
    ListNode b = headB;
    while(a != b){
        a = a != null ? a.next : headB;
        b = b != null ? b.next : headA;
    }
    return a;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值