• + 0 comments

    My Java solution with linear time complexity and constant space complexity:

    static boolean compareLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
            if(head1 == null && head2 == null) return true;
            
            while(head1 != null && head2 != null){
                if(head1.data != head2.data) return false;
                 
                 head1 = head1.next;
                 head2 = head2.next;
            }
            return head1 == head2;
        }