{"id":"0ed5a482-7df7-4b26-bec0-ccb2de29f946","name":"Subtract 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. Subtract 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\n4. any list can be larger in term of number.","inputFormat":"1->2->3->4->5->6->7->null\r\n7->8->9->null\r\n","outputFormat":"1->2->3->3->7->7->8->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* subtractTwoNumbers(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 = subtractTwoNumbers(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 subtractTwoNumbers(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 = subtractTwoNumbers(head1, head2);\r\n printList(ans);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1 8 0 5 4 6 \r\n1\r\n1 ","sampleOutput":"1 8 0 5 4 5","questionVideo":"https://www.youtube.com/embed/U5Ix-rscbl4","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":"f1b476a0-a1bb-4294-a435-243687e361ab","name":"Subtract Two Linkedlist","slug":"subtract-two-linkedlist","type":1}],"next":{"id":"968b6a4d-b682-44a1-be55-092e9d37b1df","name":"Subtract Two Linkedlist MCQ","type":0,"slug":"subtract-two-linkedlist-mcq"},"prev":{"id":"35c2c789-98db-445d-b2bd-6815d92d612a","name":"Add Two Linked list","type":3,"slug":"add-two-linked-list"}}}

Subtract Two Linkedlist

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. Subtract 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. 4. any list can be larger in term of number.

{"id":"0ed5a482-7df7-4b26-bec0-ccb2de29f946","name":"Subtract 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. Subtract 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\n4. any list can be larger in term of number.","inputFormat":"1->2->3->4->5->6->7->null\r\n7->8->9->null\r\n","outputFormat":"1->2->3->3->7->7->8->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* subtractTwoNumbers(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 = subtractTwoNumbers(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 subtractTwoNumbers(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 = subtractTwoNumbers(head1, head2);\r\n printList(ans);\r\n }\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1 8 0 5 4 6 \r\n1\r\n1 ","sampleOutput":"1 8 0 5 4 5","questionVideo":"https://www.youtube.com/embed/U5Ix-rscbl4","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":"f1b476a0-a1bb-4294-a435-243687e361ab","name":"Subtract Two Linkedlist","slug":"subtract-two-linkedlist","type":1}],"next":{"id":"968b6a4d-b682-44a1-be55-092e9d37b1df","name":"Subtract Two Linkedlist MCQ","type":0,"slug":"subtract-two-linkedlist-mcq"},"prev":{"id":"35c2c789-98db-445d-b2bd-6815d92d612a","name":"Add Two Linked list","type":3,"slug":"add-two-linked-list"}}}
plane

Editor


Loading...

Subtract Two Linkedlist

medium

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. Subtract 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. 4. any list can be larger in term of number.

Constraints

0 <= N <= 10^6

Format

Input

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

Output

1->2->3->3->7->7->8->null

Example

Sample Input

6 1 8 0 5 4 6 1 1

Sample Output

1 8 0 5 4 5

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode