[剑指offer][Java]两个链表的第一个公共节点

2019-07-06  本文已影响0人  Maxinxx

题目

输入两个链表,找出它们的第一个公共结点。

程序核心思想

首先判断这个两个链表有没有环。https://www.jianshu.com/p/2d229077ce10

Tips

注意链表为空的情况,要返回null,否则会出现空指针异常。

代码

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode loop1 = getLoop(pHead1);
        ListNode loop2 = getLoop(pHead2);
        
        if(loop1 == null && loop2 == null){
            return noLoop(pHead1, pHead2);
        }
        if(loop1 != null && loop2 != null){
            return bothLoop(pHead1, pHead2, loop1, loop2);
        }
        return null;
    }
    
    public ListNode getLoop(ListNode head){
        if(head == null)    return null;
        ListNode slow = head;
        ListNode fast = head;
        if(slow.next != null){
            slow = slow.next;
        }else{
            return null;
        }
        if(slow.next != null){
            fast = slow.next;
        }else{
            return null;
        }
        
        while(fast.next != null && fast.next.next != null){
            if(fast != slow){
                fast = fast.next.next;
                slow = slow.next;
            }else{
                fast = head;
                while(true){
                    if(fast == slow){
                        return fast;
                    }else{
                        fast = fast.next;
                        slow = slow.next;
                    }
                }
            }
        }
        return null;
    }
    
    public int getLen(ListNode head){
        int len = 0;
        while(head != null){
            len++;
            head = head.next;
        }
        return len;
    }
    
    public int getLen(ListNode head, ListNode end){
        int len = 0;
        while(head != end){
            len++;
            head = head.next;
        }
        return len + 1;
    }
    
    public ListNode noLoop(ListNode headA, ListNode headB){
        int len1 = getLen(headA);
        int len2 = getLen(headB);
        return NoLoop(headA, headB, len1, len2);
    }
    
    public ListNode NoLoop(ListNode headA, ListNode headB, int len1, int len2){
        if(len1 > len2){
            for(int i = 0; i < len1 - len2; i++){
                headA = headA.next;
            }
        }else{
            for(int i = 0; i < len2 - len1; i++){
                headB = headB.next;
            }
        }
        while(headA != null){
                if(headA != headB){
                    headA = headA.next;
                    headB = headB.next;
                }else{
                    return headA;
                }
        }
        return null;
    }
    
    public ListNode bothLoop(ListNode headA, ListNode headB, ListNode loop1, ListNode loop2){
        int len1 = getLen(headA, loop1);
        int len2 = getLen(headB, loop2);
        if(loop1 == loop2){
            return NoLoop(headA, headB, len1, len2);
        }else{
            ListNode temp = loop1.next;
            while(temp != loop1){
                if(temp == loop2){
                    return len1 > len2 ? loop2 : loop1;
                }else{
                    temp = temp.next;
                }
            }
            return null;
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读