{"id":"1615e1f7-ce8d-45db-b81e-fcb53ff33cd1","name":"Multiply Two Linkedlist","description":"1. You are given two single linkedlist of digits. \r\n2. The most significant digit comes first and each of their nodes contain a single digit. Multiply the two numbers and return it as a linked list.\r\n3. You may assume the two numbers do not contain any leading zero, except the number 0 itself.\r\n","inputFormat":"1->2->3->4->5->null\r\n7->8->9->null\r\n","outputFormat":"9->7->4->0->2->0->6->null\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *midNode(ListNode *head)\r\n{\r\n if (head == nullptr || head->next == nullptr)\r\n return head;\r\n\r\n ListNode *slow = head;\r\n ListNode *fast = head;\r\n while (fast->next != nullptr && fast->next->next != nullptr)\r\n {\r\n slow = slow->next;\r\n fast = fast->next->next;\r\n }\r\n\r\n return slow;\r\n}\r\n\r\nListNode *multiplyTwoLL(ListNode *l1, ListNode *l2)\r\n{\r\n return nullptr;\r\n}\r\n\r\n// Input_code===================================================\r\n\r\nListNode *makeList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *head1 = makeList(n);\r\n\r\n int m;\r\n cin >> m;\r\n ListNode *head2 = makeList(m);\r\n\r\n ListNode *head = multiplyTwoLL(head1, head2);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode multiplyTwoLL(ListNode l1, ListNode l2) {\r\n return null;\r\n }\r\n\r\n // InFput_code===================================================\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode makeList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n ListNode head1 = makeList(scn.nextInt());\r\n ListNode head2 = makeList(scn.nextInt());\r\n\r\n ListNode ans = multiplyTwoLL(head1, head2);\r\n printList(ans);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n6 1 3 2 4 0 \r\n2\r\n3 5 \r\n","sampleOutput":"2 1 4 6 3 4 0 0 ","questionVideo":"https://www.youtube.com/embed/I5uKH3BWsLc","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"54aede25-48b0-4ae3-a788-4f3eada019d4","name":"Multiply Two Linkedlist","slug":"multiply-two-linkedlist","type":1}],"next":{"id":"fc847e86-a947-4043-8914-b38f3c7f57ba","name":"Left View Of A Binary Tree","type":1,"slug":"left-view-of-a-binary-tree"},"prev":{"id":"968b6a4d-b682-44a1-be55-092e9d37b1df","name":"Subtract Two Linkedlist MCQ","type":0,"slug":"subtract-two-linkedlist-mcq"}}}

Multiply Two Linkedlist

1. You are given two single linkedlist of digits. 2. The most significant digit comes first and each of their nodes contain a single digit. Multiply the two numbers and return it as a linked list. 3. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

{"id":"1615e1f7-ce8d-45db-b81e-fcb53ff33cd1","name":"Multiply Two Linkedlist","description":"1. You are given two single linkedlist of digits. \r\n2. The most significant digit comes first and each of their nodes contain a single digit. Multiply the two numbers and return it as a linked list.\r\n3. You may assume the two numbers do not contain any leading zero, except the number 0 itself.\r\n","inputFormat":"1->2->3->4->5->null\r\n7->8->9->null\r\n","outputFormat":"9->7->4->0->2->0->6->null\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *midNode(ListNode *head)\r\n{\r\n if (head == nullptr || head->next == nullptr)\r\n return head;\r\n\r\n ListNode *slow = head;\r\n ListNode *fast = head;\r\n while (fast->next != nullptr && fast->next->next != nullptr)\r\n {\r\n slow = slow->next;\r\n fast = fast->next->next;\r\n }\r\n\r\n return slow;\r\n}\r\n\r\nListNode *multiplyTwoLL(ListNode *l1, ListNode *l2)\r\n{\r\n return nullptr;\r\n}\r\n\r\n// Input_code===================================================\r\n\r\nListNode *makeList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *head1 = makeList(n);\r\n\r\n int m;\r\n cin >> m;\r\n ListNode *head2 = makeList(m);\r\n\r\n ListNode *head = multiplyTwoLL(head1, head2);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode multiplyTwoLL(ListNode l1, ListNode l2) {\r\n return null;\r\n }\r\n\r\n // InFput_code===================================================\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode makeList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n ListNode head1 = makeList(scn.nextInt());\r\n ListNode head2 = makeList(scn.nextInt());\r\n\r\n ListNode ans = multiplyTwoLL(head1, head2);\r\n printList(ans);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n6 1 3 2 4 0 \r\n2\r\n3 5 \r\n","sampleOutput":"2 1 4 6 3 4 0 0 ","questionVideo":"https://www.youtube.com/embed/I5uKH3BWsLc","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"54aede25-48b0-4ae3-a788-4f3eada019d4","name":"Multiply Two Linkedlist","slug":"multiply-two-linkedlist","type":1}],"next":{"id":"fc847e86-a947-4043-8914-b38f3c7f57ba","name":"Left View Of A Binary Tree","type":1,"slug":"left-view-of-a-binary-tree"},"prev":{"id":"968b6a4d-b682-44a1-be55-092e9d37b1df","name":"Subtract Two Linkedlist MCQ","type":0,"slug":"subtract-two-linkedlist-mcq"}}}
plane

Editor


Loading...

Multiply Two Linkedlist

easy

1. You are given two single linkedlist of digits. 2. The most significant digit comes first and each of their nodes contain a single digit. Multiply the two numbers and return it as a linked list. 3. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Constraints

0 <= N <= 10^6

Format

Input

1->2->3->4->5->null 7->8->9->null

Output

9->7->4->0->2->0->6->null

Example

Sample Input

6 6 1 3 2 4 0 2 3 5

Sample Output

2 1 4 6 3 4 0 0

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode