Sort by

recency

|

995 Discussions

|

  • + 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
    def compare_lists(llist1, llist2):
        while llist1 or llist2:
            if llist1.data == llist2.data and (llist1_count == llist2_count):
                llist1 = llist1.next
                llist2 = llist2.next
            else:
                return 0
        return 1
    
  • + 0 comments
    bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
        while(head1 && head2 && head1->data == head2->data){
            head1 = head1->next;
            head2 = head2->next;
        }
        if(head1 || head2){
            return false;
        }
        return true;
    }
    
  • + 0 comments

    Solution in C

    bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
        SinglyLinkedListNode* local_head1 = head1;
        SinglyLinkedListNode* local_head2 = head2;
        while(local_head1 != NULL && local_head2 != NULL)
        {
                if(local_head1->data == local_head2->data)
                {
                        local_head1 = local_head1->next;
                        local_head2 = local_head2->next;
    
                }
                else {
                    return 0;
                }
            if(local_head1 != NULL && local_head2 != NULL){
            }
            else
            {
                if(local_head1 == NULL && local_head2 == NULL) return 1;
                else return 0;
            }
          
        }
        return 1;
        }
    
  • + 1 comment

    I don't get whats wrong, the first test case always fails.

    bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
    
        while(head1->next!=NULL && head2->next!=NULL)
        {
            if(head1->data!=head2->data)
            {
                return 0;
                break;
            }
                
            head1=head1->next;
            head2=head2->next;
        }
        return 1;
    }