160-Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:           a1 → a2
                      ↘
                        c1 → c2 → c3
                      ↗            
B:       b1 → b2 → b3
begin to intersect at node c1.

题目很简单就是找两个交叉链表的交点。如果没有就返回null

正常的思路是, 先获取到两个链表的长度 l1,l2 . 然后将长的链表 先移动 |l1 -l2|长度, 然后在一起移动如果有相等的节点 那就是交点
下面是我的代码...一次写的比较丑。

/**
 * 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 lenA = getListLen(headA);
        int lenB = getListLen(headB);
        
        
        ListNode correct = (lenA > lenB) ? headA : headB;
        ListNode other = (correct == headA) ? headB : headA;
        
        int correctLen = Math.abs(lenA - lenB);
        while(correctLen != 0){
            correct = correct.next;
            correctLen--;
        }
        
        while(correct != null && other != null){
            if(correct == other){
                return correct;
            }
            correct = correct.next;
            other = other.next;
        }
        return null;
    }
    
    private int getListLen(ListNode head){
        int i = 0;
        ListNode temp = head;
        while(temp != null){
            i++;
            temp = temp.next;
        }
        return i;
    }
}

有趣的是,有大神提出了 不需要获取长度的算法 让我膝盖一跪。
简单的来说或就是先同时遍历两个链表:
一种情况是 长度为 a 和 b (a < b)
当 a 到头的时候 a 换成 b链表 和b同时开始。当b换到a链表的时候 a又在b链表上移动了 (b - a)的距离。
所以这个时候 就相等于前面的 “先移动 |l1 -l2|长度” 的动作了后续就比较又没交叉节点就好了。

@hpplayer said in Java solution without knowing the difference in len!:

I found most solutions here preprocess linkedlists to get the difference in len.
Actually we don't care about the "value" of difference, we just want to make sure two pointers reach the intersection node at the same time.

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

Below is my commented Java code:

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;
}

你可能感兴趣的:(160-Intersection of Two Linked Lists)