Question
C++
Youre given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if
Output Format Compare the two linked lists and return 1 if the lists are equal. Otherwise, return 0. Do NOT print anything to
1 #include <bits/stdc++.h>. 59 // Complete the compare_lists 60 function below. 61 62 For your reference: 63 64 SinglyLinked
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:-

According to your question, I just provide compare_lists function and, for your better understanding I will provide the screenshot of sample code where compare_lists function is used to compare the two lists.

Algo. for Compare Two Lists:-

  1. Traverse both lists parallelly and check each list should not be NULL
  2. Check each data value of each list should be equal.
  3. Repeat 1 and 2 step untile the list not became to NULL
  4. Finally, check if both lists are NULL then it is equal other wise it is not equal.

Code for Function:-

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

Add a comment
Know the answer?
Add Answer to:
C++ You're given the pointer to the head nodes of two linked lists. Compare the data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode {...

    Codelab question! This is c++. Consider the following code: // Linked Lists: TRAVERSE struct ListNode { int data; struct ListNode *next; }; Assume that a linked list has been created and head points to a sentinel node. A sentinel node is an empty data node in the beginning of the list. It sometimes holds a sentinel value. The use of sentinel nodes is a popular trick to simplify the insert and delete operations. You may also assume that the list...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha *...

    Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha * Definition for singly-linked list. * public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution { public ListNode merge TwoLists(ListNode li, ListNode 12) { 10 Example: Input: 1->2->4, 1-3-4...

  • JAVA lists and nodes my answer was : ListNode newNode = new ListNode(3); newNode.next = head;...

    JAVA lists and nodes my answer was : ListNode newNode = new ListNode(3); newNode.next = head; head = newNode; it is incorrect. what could it be? linkedNodes 11 > <linkedNodes 9 Main Page -- Problems - Solve a Problem OBJP5 Self-Check 16.10: linkedNodes 10 Language/Type: Java ListNodes LinkedLists Related Links: LinkedIntList.java Author: Marty Stepp (on 2019/09/19) Write the code necessary to convert the following sequence of ListNode objects: list -> [1] -> [2] / Into this sequence of ListNode objects:...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • 1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head...

    1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head reference refers to the first node contains an Integer with value 4. If Node curr = head; curr= curr.next; are applied to this list, what is the number you can find in the first node? 2) Given a singly linked list contains 6 nodes, which is simply shown as 1->2->3->4->5->6. Assume head refers to the first node (contains 1) on the list. How many...

  • c++ 5. You're given the pointer to the head node of a sorted linked list, where...

    c++ 5. You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty. Enter numbers ending with -999 for the linked list: 3 7 11 11 11 24 27 30 42 42 47 55 55 78 89-999...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove...

    C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. (i.e. n is greater than 0) Follow up: Could you do this in one pass? Hint: Maintain two pointers and update one with...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT