Java solution used hash table,O(n)


  • 0
    X

    public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode head =null;
    Set<ListNode> a = new HashSet<>();
    while(headA!=null){
    a.add(headA);
    headA = headA.next;
    }
    while(headB!=null){
    if(a.contains(headB)){
    head = headB;
    break;
    }else{
    headB=headB.next;
    }
    }
    return head;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.