`{"id":"797131c8-a39b-4620-abb2-507b7e150790","name":"Add Two Linkedlist","description":"1. You are give two single linkedlist of digits. \r\n2. The most significant digit comes first and each of their nodes contain a single digit. Add 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.","inputFormat":"1->2->3->4->5->6->7->null\r\n7->8->9->null","outputFormat":"1->2->3->5->3->5->6->null","constraints":"0 &lt;= N &lt;= 10^6","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 *addTwoNumbers(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 = addTwoNumbers(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 addTwoNumbers(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 = addTwoNumbers(head1, head2);\r\n printList(ans);\r\n }\r\n\r\n}"},"python":{"code":"print()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n6 1 3 2 4 0 \r\n2\r\n3 5","sampleOutput":"6 1 3 2 7 5","questionVideo":"","hints":[],"associated":[{"id":"7c88f029-1bfa-445a-8d6f-96d5b8f62b9b","name":"What is the the most significant digit and the least significant digit in the following number? 2382187218(Q_ ATL)","slug":"what-is-the-the-most-significant-digit-and-the-least-significant-digit-in-the-following-number-2382187218-q-atl","type":4},{"id":"f614dad7-fe32-472a-88a4-a0f3bb660b60","name":"What is the time complexity of the recursive solution for this problem?(Q- ATL)","slug":"what-is-the-time-complexity-of-the-recursive-solution-for-this-problem-q-atl","type":4}],"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":"2354569d-023f-4ca0-b94f-f6bac8ae4f06","name":"Add Two Linkedlist","slug":"add-two-linkedlist","type":1}],"next":{"id":"841532e4-ba86-405d-a408-64da62b0db82","name":"Add Two Linkedlist MCQ","type":0,"slug":"add-two-linkedlist-mcq"},"prev":{"id":"ea8e4512-8ee2-4c42-b73f-481e7dc64568","name":"Subtract Two Linked Lists","type":3,"slug":"subtract-two-linked-lists"}}}`

1. You are give two single linkedlist of digits. 2. The most significant digit comes first and each of their nodes contain a single digit. Add 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":"797131c8-a39b-4620-abb2-507b7e150790","name":"Add Two Linkedlist","description":"1. You are give two single linkedlist of digits. \r\n2. The most significant digit comes first and each of their nodes contain a single digit. Add 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.","inputFormat":"1->2->3->4->5->6->7->null\r\n7->8->9->null","outputFormat":"1->2->3->5->3->5->6->null","constraints":"0 &lt;= N &lt;= 10^6","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 *addTwoNumbers(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 = addTwoNumbers(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 addTwoNumbers(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 = addTwoNumbers(head1, head2);\r\n printList(ans);\r\n }\r\n\r\n}"},"python":{"code":"print()"}},"points":10,"difficulty":"easy","sampleInput":"6\r\n6 1 3 2 4 0 \r\n2\r\n3 5","sampleOutput":"6 1 3 2 7 5","questionVideo":"","hints":[],"associated":[{"id":"7c88f029-1bfa-445a-8d6f-96d5b8f62b9b","name":"What is the the most significant digit and the least significant digit in the following number? 2382187218(Q_ ATL)","slug":"what-is-the-the-most-significant-digit-and-the-least-significant-digit-in-the-following-number-2382187218-q-atl","type":4},{"id":"f614dad7-fe32-472a-88a4-a0f3bb660b60","name":"What is the time complexity of the recursive solution for this problem?(Q- ATL)","slug":"what-is-the-time-complexity-of-the-recursive-solution-for-this-problem-q-atl","type":4}],"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":"2354569d-023f-4ca0-b94f-f6bac8ae4f06","name":"Add Two Linkedlist","slug":"add-two-linkedlist","type":1}],"next":{"id":"841532e4-ba86-405d-a408-64da62b0db82","name":"Add Two Linkedlist MCQ","type":0,"slug":"add-two-linkedlist-mcq"},"prev":{"id":"ea8e4512-8ee2-4c42-b73f-481e7dc64568","name":"Subtract Two Linked Lists","type":3,"slug":"subtract-two-linked-lists"}}}`

Editor

easy

1. You are give two single linkedlist of digits. 2. The most significant digit comes first and each of their nodes contain a single digit. Add 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.

0 <= N <= 10^6

## Format

### Input

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

### Output

1->2->3->5->3->5->6->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 6 1 3 2 4 0 2 3 5```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}6 1 3 2 7 5`

Discussions

Show Discussion

Related Resources