Sort by

recency

|

1002 Discussions

|

  • + 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;
        }
    
  • + 0 comments

    Python 3.10+

    def compare_lists(llist1: SinglyLinkedListNode, llist2: SinglyLinkedListNode) -> bool:
        """Iteratively check two linked lists for equality."""
        while llist1 and llist2 and llist1.data == llist2.data:
            llist1 = llist1.next
            llist2 = llist2.next
        return llist1 is None and llist2 is None
    
    
    def compare_lists(llist1: SinglyLinkedListNode, llist2: SinglyLinkedListNode) -> bool:
        """Recursively check two linked lists for equality."""
        match llist1 is None, llist2 is None:
            case False, False:
                return (
                    compare_lists(llist1.next, llist2.next)
                    if llist1.data == llist2.data
                    else False
                )
            case True, True:
                return True
            case _:
                return False
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/sxWBiWg16JU

    bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
        while(head1 != nullptr && head2 !=nullptr) {
            if(head1->data != head2->data) return false;
            head1 = head1 ->next;
            head2 = head2 ->next;
        }
        return head2 == head1;
    }
    
  • + 0 comments

    Python solution:

    def compare_lists(llist1, llist2):

    current1=llist1
    current2=llist2
    flag=True
    while current1 and current2 and flag is True: 
        if current1.data != current2.data:
            flag=False
        current1=current1.next
        current2=current2.next
    
    if flag is True and current1==current2:
        return 1
    else:
        return 0
    
  • + 0 comments

    Solution in C

    bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
            SinglyLinkedListNode* curr1 = head1;
            SinglyLinkedListNode* curr2 = head2;
    
            if (head1 == NULL && head2 == NULL) {return true;}
            if (head1 == NULL || head2 == NULL) {return false;}
    
            while (curr1 != NULL && curr2 != NULL) {
                    if (curr1->data != curr2->data) {
                            return false;
                    }
    
                    curr1 = curr1->next;
                    curr2 = curr2->next;
            }
    
    return (curr1 == NULL && curr2 == NULL);
    

    }