Question

Could someone help me out with the answering this? Explaining the theory would be very helpful

A NumberList is a linked list used to represent the digits of a number. For example 983 is represented as: [ Node(9), Node(8)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Problem statement :

Adding two link lists which have their nodes rep// Jdvd Piuyi dill LU duuLWU lumbers resulL UY LIeu CISL class NumberList { static Node headı, head2; static class Node { intreturn result /* Display linked list */ void displayList (Node head) while head is not null Print head. data head := head. neresenting two numbers.

Pseudo code :

 // Java program to add two numbers result by linked list class NumberList { static Node head1, head2; static class Node { int data; Node next; Node(int d) { data = d; next = null; } } /* Adds digits of two linked lists and return the head node of resultult list */ Node sumLists(Node list1, Node list2) { Node result := null // result is head node of the result list Node previous := null Node temp := null int carry := 0, sum while list1 is not null or list2 is not null //traversing end of both the lists // Sum each digits of list1 and list2 in result list. sum = carry + (list1 != null ? list1.data : 0) + (list2 != null ? list2.data : 0) // carry update carry := (sum >= 10) ? 1 : 0 // sum update sum := sum % 10 // Create a new node with sum as data temp := new Node(sum) // if this is the list1 node then set it as head of // the result list if result is null result := temp else // If this is not the list1 node then connect it to the resultt. previous.next := temp // Set previous for next insertion previous := temp // Move list1 and list2 pointers to next nodes if list1 is not null list1 = list1.next if list2 is not null list2 := list2.next if carry > 0 temp.next := new Node(carry) // return head of the result list return result } /* Display linked list */ void displayList(Node head) { while head is not null Print head.data head := head.next } main() { NumberList list = new NumberList(); // creating list1 list.head1 = new Node(9) list.head1.next = new Node(8) list.head1.next.next = new Node(3) print "List1" list.displayList(head1) // creating list2 list.head2 = new Node(1) list.head2.next = new Node(0) list.head1.next.next = new Node(4) list.head1.next.next.next = new Node(3) print "List2" list.displayList(head2); // add the two lists in result list Node resultList = list.addTwoLists(head1, head2) print "Result List" list.printList(resultList) } } 

Explaination:

Traverse both lists and add value of each list nodes. If the sum is more than 10 then carry over 1 and reduce sum. If one list has more elements than the other then take remaining values of this list as 0.

Add a comment
Know the answer?
Add Answer to:
Could someone help me out with the answering this? Explaining the theory would be very helpful...
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
  • Can you please help with the below? 1)   Which of the following is true about using...

    Can you please help with the below? 1)   Which of the following is true about using a 2-3-4 tree? a.   It is designed to minimize node visits while keeping to an O(log n) search performance b.   It is designed to self-balance as new values are inserted into the tree c.   As soon as a node becomes full, it performs the split routine d.   None of the above 2)   Which of the following is true about a binary search tree? a.  ...

  • if someone could show me how to do these problems it would be very helpful. Orate...

    if someone could show me how to do these problems it would be very helpful. Orate the major products) or pronde the reagents for each reachan below lise bold and dashed wedges to show the proper stereochemistry where necessary Write both the O- and p- products where necessary (3pts each answer). Br (A) NILE NBS Kinenc Product Thermodynamio Product CHCH3 X CH₂ CH3 Na liquid Nitz (E) HO CH-C-CHE 0 etts CHg thay chci AlC3 hsou (G) NO₂ Chzical AlCl3...

  • l have posted it a few times before but didnt get a satisfactory answer. kindly help me by answering in pseudo cod...

    l have posted it a few times before but didnt get a satisfactory answer. kindly help me by answering in pseudo code 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . . . 1"} with weight {w1·W2. . . . . ux) and value (n 2, .., nJ, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity...

  • In this assignment, you will use a Hashtable to determine the common elements in all the...

    In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....

  • I need someone to help me out with homework in develop marketing strategies please. By answering...

    I need someone to help me out with homework in develop marketing strategies please. By answering the following numbered questions in social marketing to complete it. Please!!! Focus: To increase adult immunization rate in the state of New Hampshire, using the parent as the target audience. Questions: 1. Product: Creating the product platform 2. Core Product: What is the major perceived benefit your target audience wants from performing the behavior that you will highlight? 3. Actual Product: What, if any,...

  • Someone help me with the following questions using java Canvas x New Tab Help Center Submitted...

    Someone help me with the following questions using java Canvas x New Tab Help Center Submitted Dec 3 at T0:06pm This attempt took 3 minutes. Question 1 0/0.5 Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorit Index 0 Value 12 18 4, 9, 12.2.6,8.18 9, 4, 12, 2,6,18,8 2,4,9,12, 6, 8. 18 2. 4, 6, 8, 9,12,18 2. 4, 12,9.6,8,18 0/0.5 pts Question 2 the array after...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are...

    I only need help with the constructors and add method. Thanks! Background Arbitrary length integers are integers that have no size restriction. Recall that the int type has a range of -2,147,483,648 to 2,147,483,647. The long type runs from -263 to 263 - 1. Sometimes these numbers are not large enough. One example application that may need larger numbers is public-key cryptography where very large integers are used to make decryption hard. A large integer may be represented as a...

  • help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow...

    help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Exercises: 1) Which of the following applications may use a stack? A. parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above. 2) Consider the usual algorithm for determining whether a sequence of parentheses is balanced....

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

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