Amazon Interview Question

2) Multiply Two linked list data

Interview Answer

Anonymous

Jan 21, 2016

// Function to multiply two given lists // return resulted list static Node multiply(Node head, Node head1) { // Base Condition if (head == null || head1 == null) return null; int number = getNumber(head); // convert one list into number // traverse the second list and multiply the number with the current // element of the list and store in the new list. Node current = head1; Node result = null; while (current != null) { if (result == null) { result = new Node(current.data * number); } else { // multiply current element with the "number" and store in the // new list node Node temp = new Node(current.data * number); temp.next = result; result = temp; } current = current.next; } return process(result); }