`{"id":"1a583175-428b-4936-b764-0ec1db0e81f5","name":"Subtract Two Linked Lists","description":"1. You are given two linked lists with N and M nodes respectively.\r\n 2. The linked list as a whole represents a large positive number.\r\n 3. You have to write a function that subtracts the smaller number\r\n from larger one and returns a pointer to the resultant linked list.\r\n 4. display is a utility function which displays the contents of Linked List,\r\n feel free to use it for debugging purposes.\r\n 5. main takes input from the users and creates the Linked Lists. You can use\r\n display to know its contents.\r\n 6. This is a functional problem. \r\n 7. You should code only the sublinkedList function. It takes as input the\r\n heads of the first and second linked list respectively. It should find the\r\n difference of two linked lists and return a pointer to the resultant linked list.\r\n 8. Don't change the code of Node, main and display.","inputFormat":"First line takes N, the number of elements in the first list.\r\n Second line takes input N space separated numbers reperesenting elements of the first linked list.\r\n Third line takes M, the number of elements in the second list.\r\n Fourth line takes input M space separated numbers reperesenting elements of the second linked list.\r\n Input is handled for you.","outputFormat":"Difference of two linked lists.\r\n Output is handled for you.","constraints":"1 &lt;= N &lt;= 1000\r\n 1 &lt;= M &lt;= 1000","sampleCode":{"cpp":{"code":"#include<iostream>\r\n #include<vector>\r\n using namespace std;\t \r\n \t \r\n // Class declaration for a Node of the Linked List\r\n \t class Node {\r\n \t public:\r\n \t int data;\r\n \t Node* next;\r\n \t \r\n \t Node(int data) {\r\n \t this->data = data;\r\n \t this->next = NULL;\r\n \t }\r\n \t };\r\n \r\n \r\n \t Node* head1=NULL;\r\n \t Node* head2=NULL;\r\n \t Node* head3=NULL;\r\n \r\n /*\r\n * Input Parameters: head: head of the linked list in which a new node is to\r\n * be inserted. data: the data value of the node which is to be inserted.\r\n * \r\n * Return Value: head of the linked list in which the node is inserted\r\n */\r\n Node* insert(Node* head, int data) {\r\n \r\n if (head == NULL) {\r\n Node* nn = new Node(data);\r\n return nn;\r\n }\r\n \r\n head->next = insert(head->next, data);\r\n return head;\r\n }\r\n \r\n void display(Node* head) {\r\n for (Node* node = head; node != NULL; node = node->next) {\r\n cout<<node->data<<\" \";\r\n }\r\n }\r\n // -----------------------------------------------------\r\n // This is a functional problem. Only this function has to be written.\r\n // This function takes as input the head of two linked lists. \r\n // This function should return the head of the resultant linked list.\r\n Node* sublinkedList(Node* l1, Node* l2) {\r\n // write your code here\r\n }\r\n \r\n \r\n // -----------------------------------------------------\r\n \r\n \r\n \t \r\n int main(int argc, char** argv) {\r\n int n1;\r\n cin>>n1;\r\n \r\n for (int i = 0; i < n1; i++) {\r\n int a;\r\n cin>>a;\r\n head1 = insert(head1, a);\r\n }\r\n \r\n int n2;\r\n cin>>n2;\r\n \r\n for (int i = 0; i < n2; i++) {\r\n int a;\r\n cin>>a;\r\n head2 = insert(head2, a);\r\n }\r\n \t \t head3 = sublinkedList(head1, head2);\r\n display(head3);\r\n \r\n \r\n }"},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n // -----------------------------------------------------\r\n // This is a functional problem. Only this function has to be written.\r\n // This function takes as input the head of two linked lists. \r\n // This function should return the head of the resultant linked list.\r\n public static Node sublinkedList(Node l1, Node l2) {\r\n // write your code here\r\n }\r\n \r\n \r\n // -----------------------------------------------------\r\n \r\n public static void main(String[] args) {\r\n Scanner sc = new Scanner(System.in);\r\n int n = sc.nextInt();\r\n int a1 = sc.nextInt();\r\n head = insert(head, a1);\r\n \r\n for (int i = 1; i < n; i++) {\r\n int a = sc.nextInt();\r\n head = insert(head, a);\r\n }\r\n \r\n int n2 = sc.nextInt();\r\n int a2 = sc.nextInt();\r\n head2 = insert(head2, a2);\r\n \r\n for (int i = 1; i < n2; i++) {\r\n int a = sc.nextInt();\r\n head2 = insert(head2, a);\r\n }\r\n \r\n head3 = sublinkedList(head, head2);\r\n display(head3);\r\n \r\n }\r\n \r\n // Class declaration for a Node of the Linked List\r\n static class Node {\r\n int data;\r\n Node next;\r\n \r\n public Node(int data) {\r\n this.data = data;\r\n this.next = null;\r\n }\r\n \r\n }\r\n \r\n static Node head;\r\n \r\n static Node head2;\r\n \r\n static Node head3;\r\n \r\n /*\r\n * Input Parameters: head: head of the linked list in which a new node is to\r\n * be inserted. data: the data value of the node which is to be inserted.\r\n * \r\n * Return Value: head of the linked list in which the node is inserted\r\n */\r\n public static Node insert(Node head, int data) {\r\n \r\n if (head == null) {\r\n return new Node(data);\r\n }\r\n \r\n head.next = insert(head.next, data);\r\n return head;\r\n }\r\n \r\n /*\r\n * Input Parameters: head: head of the linked list in which is to be\r\n * displayed.\r\n * \r\n * Return Value: null\r\n */\r\n public static void display(Node head) {\r\n for (Node node = head; node != null; node = node.next) {\r\n System.out.print(node.data + \" \");\r\n }\r\n }\r\n \r\n }"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n0 0 0 1 0 0 0\r\n4\r\n0 0 1 0","sampleOutput":"0 0 0 0 9 9 0","questionVideo":"","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":"112da43a-d52c-407c-bea0-eaf0de20d558","name":"Subtract Two Linked Lists","slug":"subtract-two-linked-lists","type":1}],"next":{"id":"ea8e4512-8ee2-4c42-b73f-481e7dc64568","name":"Subtract Two Linked Lists","type":3,"slug":"subtract-two-linked-lists"},"prev":{"id":"08f4f38f-110b-4cfe-a34f-b31811d36d51","name":"Is Cycle Present In Linkedlist","type":3,"slug":"is-cycle-present-in-linkedlist"}}}`

1. You are given two linked lists with N and M nodes respectively. 2. The linked list as a whole represents a large positive number. 3. You have to write a function that subtracts the smaller number from larger one and returns a pointer to the resultant linked list. 4. display is a utility function which displays the contents of Linked List, feel free to use it for debugging purposes. 5. main takes input from the users and creates the Linked Lists. You can use display to know its contents. 6. This is a functional problem. 7. You should code only the sublinkedList function. It takes as input the heads of the first and second linked list respectively. It should find the difference of two linked lists and return a pointer to the resultant linked list. 8. Don't change the code of Node, main and display.

`{"id":"1a583175-428b-4936-b764-0ec1db0e81f5","name":"Subtract Two Linked Lists","description":"1. You are given two linked lists with N and M nodes respectively.\r\n 2. The linked list as a whole represents a large positive number.\r\n 3. You have to write a function that subtracts the smaller number\r\n from larger one and returns a pointer to the resultant linked list.\r\n 4. display is a utility function which displays the contents of Linked List,\r\n feel free to use it for debugging purposes.\r\n 5. main takes input from the users and creates the Linked Lists. You can use\r\n display to know its contents.\r\n 6. This is a functional problem. \r\n 7. You should code only the sublinkedList function. It takes as input the\r\n heads of the first and second linked list respectively. It should find the\r\n difference of two linked lists and return a pointer to the resultant linked list.\r\n 8. Don't change the code of Node, main and display.","inputFormat":"First line takes N, the number of elements in the first list.\r\n Second line takes input N space separated numbers reperesenting elements of the first linked list.\r\n Third line takes M, the number of elements in the second list.\r\n Fourth line takes input M space separated numbers reperesenting elements of the second linked list.\r\n Input is handled for you.","outputFormat":"Difference of two linked lists.\r\n Output is handled for you.","constraints":"1 &lt;= N &lt;= 1000\r\n 1 &lt;= M &lt;= 1000","sampleCode":{"cpp":{"code":"#include<iostream>\r\n #include<vector>\r\n using namespace std;\t \r\n \t \r\n // Class declaration for a Node of the Linked List\r\n \t class Node {\r\n \t public:\r\n \t int data;\r\n \t Node* next;\r\n \t \r\n \t Node(int data) {\r\n \t this->data = data;\r\n \t this->next = NULL;\r\n \t }\r\n \t };\r\n \r\n \r\n \t Node* head1=NULL;\r\n \t Node* head2=NULL;\r\n \t Node* head3=NULL;\r\n \r\n /*\r\n * Input Parameters: head: head of the linked list in which a new node is to\r\n * be inserted. data: the data value of the node which is to be inserted.\r\n * \r\n * Return Value: head of the linked list in which the node is inserted\r\n */\r\n Node* insert(Node* head, int data) {\r\n \r\n if (head == NULL) {\r\n Node* nn = new Node(data);\r\n return nn;\r\n }\r\n \r\n head->next = insert(head->next, data);\r\n return head;\r\n }\r\n \r\n void display(Node* head) {\r\n for (Node* node = head; node != NULL; node = node->next) {\r\n cout<<node->data<<\" \";\r\n }\r\n }\r\n // -----------------------------------------------------\r\n // This is a functional problem. Only this function has to be written.\r\n // This function takes as input the head of two linked lists. \r\n // This function should return the head of the resultant linked list.\r\n Node* sublinkedList(Node* l1, Node* l2) {\r\n // write your code here\r\n }\r\n \r\n \r\n // -----------------------------------------------------\r\n \r\n \r\n \t \r\n int main(int argc, char** argv) {\r\n int n1;\r\n cin>>n1;\r\n \r\n for (int i = 0; i < n1; i++) {\r\n int a;\r\n cin>>a;\r\n head1 = insert(head1, a);\r\n }\r\n \r\n int n2;\r\n cin>>n2;\r\n \r\n for (int i = 0; i < n2; i++) {\r\n int a;\r\n cin>>a;\r\n head2 = insert(head2, a);\r\n }\r\n \t \t head3 = sublinkedList(head1, head2);\r\n display(head3);\r\n \r\n \r\n }"},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n // -----------------------------------------------------\r\n // This is a functional problem. Only this function has to be written.\r\n // This function takes as input the head of two linked lists. \r\n // This function should return the head of the resultant linked list.\r\n public static Node sublinkedList(Node l1, Node l2) {\r\n // write your code here\r\n }\r\n \r\n \r\n // -----------------------------------------------------\r\n \r\n public static void main(String[] args) {\r\n Scanner sc = new Scanner(System.in);\r\n int n = sc.nextInt();\r\n int a1 = sc.nextInt();\r\n head = insert(head, a1);\r\n \r\n for (int i = 1; i < n; i++) {\r\n int a = sc.nextInt();\r\n head = insert(head, a);\r\n }\r\n \r\n int n2 = sc.nextInt();\r\n int a2 = sc.nextInt();\r\n head2 = insert(head2, a2);\r\n \r\n for (int i = 1; i < n2; i++) {\r\n int a = sc.nextInt();\r\n head2 = insert(head2, a);\r\n }\r\n \r\n head3 = sublinkedList(head, head2);\r\n display(head3);\r\n \r\n }\r\n \r\n // Class declaration for a Node of the Linked List\r\n static class Node {\r\n int data;\r\n Node next;\r\n \r\n public Node(int data) {\r\n this.data = data;\r\n this.next = null;\r\n }\r\n \r\n }\r\n \r\n static Node head;\r\n \r\n static Node head2;\r\n \r\n static Node head3;\r\n \r\n /*\r\n * Input Parameters: head: head of the linked list in which a new node is to\r\n * be inserted. data: the data value of the node which is to be inserted.\r\n * \r\n * Return Value: head of the linked list in which the node is inserted\r\n */\r\n public static Node insert(Node head, int data) {\r\n \r\n if (head == null) {\r\n return new Node(data);\r\n }\r\n \r\n head.next = insert(head.next, data);\r\n return head;\r\n }\r\n \r\n /*\r\n * Input Parameters: head: head of the linked list in which is to be\r\n * displayed.\r\n * \r\n * Return Value: null\r\n */\r\n public static void display(Node head) {\r\n for (Node node = head; node != null; node = node.next) {\r\n System.out.print(node.data + \" \");\r\n }\r\n }\r\n \r\n }"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n0 0 0 1 0 0 0\r\n4\r\n0 0 1 0","sampleOutput":"0 0 0 0 9 9 0","questionVideo":"","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":"112da43a-d52c-407c-bea0-eaf0de20d558","name":"Subtract Two Linked Lists","slug":"subtract-two-linked-lists","type":1}],"next":{"id":"ea8e4512-8ee2-4c42-b73f-481e7dc64568","name":"Subtract Two Linked Lists","type":3,"slug":"subtract-two-linked-lists"},"prev":{"id":"08f4f38f-110b-4cfe-a34f-b31811d36d51","name":"Is Cycle Present In Linkedlist","type":3,"slug":"is-cycle-present-in-linkedlist"}}}`

Editor

easy

1. You are given two linked lists with N and M nodes respectively. 2. The linked list as a whole represents a large positive number. 3. You have to write a function that subtracts the smaller number from larger one and returns a pointer to the resultant linked list. 4. display is a utility function which displays the contents of Linked List, feel free to use it for debugging purposes. 5. main takes input from the users and creates the Linked Lists. You can use display to know its contents. 6. This is a functional problem. 7. You should code only the sublinkedList function. It takes as input the heads of the first and second linked list respectively. It should find the difference of two linked lists and return a pointer to the resultant linked list. 8. Don't change the code of Node, main and display.

Constraints

1 <= N <= 1000 1 <= M <= 1000

Format

Input

First line takes N, the number of elements in the first list. Second line takes input N space separated numbers reperesenting elements of the first linked list. Third line takes M, the number of elements in the second list. Fourth line takes input M space separated numbers reperesenting elements of the second linked list. Input is handled for you.

Output

Difference of two linked lists. Output is handled for you.

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;}7 0 0 0 1 0 0 0 4 0 0 1 0```

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;}0 0 0 0 9 9 0`

Discussions

Show Discussion

Related Resources