Problem 160

Intersection of Two Linked Lists

Posted by Ruizhi Ma on July 1, 2019

问题描述

https://leetcode.com/problems/intersection-of-two-linked-lists/

解决思路

身体不舒服,明天来补

问题解惑

代码

package leetcode;

public class IntersectionofTwoLinkedLists {
    public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //corner case
        if(headA == null || headB == null) return null;

        ListNode a = headA;
        ListNode b = headB;

        while (a != b){
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }

        return a;
    }
}