{"id":"70c0b04c-987d-4a4c-bf4e-17c4e4fa5ac0","name":"Remove Last In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of removeLast function. This function is supposed to add an element to the front of LinkedList. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\". \r\n4. You are required to update head, tail and size as required.\r\n5. Input and Output is managed for you. Just update the code in removeLast function.\r\n\r\nNote -> Use the code snippet and follow the algorithm discussed in question video. The judge can't \r\n force you but the intention is to teach a concept. Play in spirit of the question.\r\n","inputFormat":"input in managed for you.\r\n","outputFormat":"output in managed for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include<iostream>\nusing namespace std;\n#include<vector>\n\n\nclass DoublyLinkedList {\n public : class Node {\n public:\n int data = 0;\n Node* prev = nullptr;\n Node* next = nullptr;\n\n Node(int data) {\n this->data = data;\n }\n\n };\n\n Node* head = nullptr;\n Node* tail = nullptr;\n int size = 0;\n\n string toString() {\n string sb;\n Node* curr = this->head;\n sb += \"[\";\n while (curr != nullptr) {\n sb += (to_string)(curr->data);\n if (curr->next != nullptr)\n sb+=\", \";\n curr = curr->next;\n }\n sb += \"]\";\n\n return sb;\n }\n\n // Exceptions========================================\n\n private:bool ListIsEmptyException() {\n if (this->size == 0) {\n cout << \"ListIsEmpty: \";\n return true;\n }\n return false;\n }\n // BasicFunctions======================================\n\n public:int Size() {\n // write your code here\n return size;\n }\n\n public:bool isEmpty() {\n // write your code here\n return size==0;\n }\n\n // AddFunctions======================================\n\n public:void addFirstNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n node->next = this->head;\n this->head->prev = node;\n this->head = node;\n }\n this->size++;\n }\n\n public: void addFirst(int val) {\n Node* node = new Node(val);\n addFirstNode(node);\n }\n\n public:void addLastNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n this->tail->next = node;\n node->prev = this->tail;\n this->tail = node;\n }\n this->size++;\n }\n\n public:void addLast(int val) {\n Node* node = new Node(val);\n addLastNode(node);\n }\n\n // RemoveFunctions======================================\n\n public:Node *removeFirstNode() {\n Node* node = this->head;\n if (this->size == 1)\n this->head = this->tail = nullptr;\n else {\n Node* nextNode = this->head->next;\n nextNode->prev = nullptr;\n node->next = nullptr;\n\n this->head = nextNode;\n }\n\n this->size--;\n return node;\n }\n\n public:int removeFirst() {\n if (ListIsEmptyException())\n return -1;\n Node* node = removeFirstNode();\n return node->data;\n }\n\n public:Node* removeLastNode() {\n Node* node = this->tail;\n if (this->size == 1)\n this->head = this->tail = nullptr;\n else {\n Node* prevNode = this->tail->prev;\n prevNode->next = nullptr;\n node->prev = nullptr;\n\n this->tail = prevNode;\n }\n\n this->size--;\n return node;\n }\n\n\n public:int removeLast() {\n if(ListIsEmptyException()){\n return -1 ;\n }\n Node *node = removeLastNode() ;\n return node->data ;\n }\n\n\n };\n \n\n int main() {\n DoublyLinkedList *dll = new DoublyLinkedList();\n \n string str;\n cin >> str;\n \n while(str != \"stop\"){\n int data;\n if(str == (\"addFirst\")){\n cin >> data;\n dll->addFirst(data);\n }\n else if (str == (\"addLast\")){\n cin >> data;\n dll->addLast((data));\n }\n else if (str == (\"removeFirst\"))\n cout << (dll->removeFirst()) << endl;\n else if (str == (\"removeLast\"))\n cout << (dll->removeLast()) << endl;\n cin >> str;\n }\n cout << dll->toString();\n \n return 0; \n }"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n\r\n public static class DoublyLinkedList {\r\n private class Node {\r\n int data = 0;\r\n Node prev = null;\r\n Node next = null;\r\n\r\n Node(int data) {\r\n this.data = data;\r\n }\r\n\r\n }\r\n\r\n private Node head = null;\r\n private Node tail = null;\r\n private int size = 0;\r\n\r\n public String toString() {\r\n StringBuilder sb = new StringBuilder();\r\n Node curr = this.head;\r\n sb.append(\"[\");\r\n while (curr != null) {\r\n sb.append(curr.data);\r\n if (curr.next != null)\r\n sb.append(\", \");\r\n curr = curr.next;\r\n }\r\n sb.append(\"]\");\r\n\r\n return sb.toString();\r\n }\r\n\r\n // Exceptions========================================\r\n\r\n private boolean ListIsEmptyException() {\r\n if (this.size == 0) {\r\n System.out.print(\"ListIsEmpty: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // AddFunctions======================================\r\n\r\n private void addFirstNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n node.next = this.head;\r\n this.head.prev = node;\r\n this.head = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addFirst(int val) {\r\n Node node = new Node(val);\r\n addFirstNode(node);\r\n }\r\n\r\n private void addLastNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n this.tail.next = node;\r\n node.prev = this.tail;\r\n this.tail = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addLast(int val) {\r\n Node node = new Node(val);\r\n addLastNode(node);\r\n }\r\n\r\n // RemoveFunctions======================================\r\n\r\n private Node removeFirstNode() {\r\n Node node = this.head;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node nextNode = this.head.next;\r\n nextNode.prev = null;\r\n node.next = null;\r\n\r\n this.head = nextNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeFirstNode();\r\n return node.data;\r\n }\r\n\r\n public int removeLast() {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n DoublyLinkedList dll = new DoublyLinkedList();\r\n\r\n String str = scn.nextLine();\r\n while (!str.equals(\"stop\")) {\r\n String[] s = str.split(\" \");\r\n if (s[0].equals(\"addFirst\"))\r\n dll.addFirst(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"addLast\"))\r\n dll.addLast(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"removeFirst\"))\r\n System.out.println(dll.removeFirst());\r\n else if (s[0].equals(\"removeLast\"))\r\n System.out.println(dll.removeLast());\r\n str = scn.nextLine();\r\n }\r\n\r\n System.out.println(dll);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"addFirst 4\r\naddFirst 4\r\naddLast 5\r\naddFirst 7\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveLast\r\nremoveFirst\r\nremoveFirst\r\nstop","sampleOutput":"7\r\n4\r\n4\r\n1\r\n5\r\nListIsEmpty: -1\r\n[]\r\n","questionVideo":"https://www.youtube.com/embed/iqe6qrKTyAc","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":"02c37ec4-3b5b-4b39-96ae-1c3b6c1ca9bf","name":"Remove Last In Doubly Linkedlist","slug":"remove-last-in-doubly-linkedlist","type":1}],"next":{"id":"e8a09549-5527-4377-bd4d-5c8502d8ff3d","name":"Remove Last In Doubly Linked List","type":3,"slug":"remove-last-in-doubly-linked-list"},"prev":{"id":"57f19c2e-b2b6-4e0d-874f-f70a077d0c37","name":"Remove First In Doubly Linked List","type":3,"slug":"remove-first-in-doubly-linked-list"}}}

Remove Last In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of removeLast function. This function is supposed to add an element to the front of LinkedList. 3. If size of list is zero then return "ListIsEmpty: -1". 4. You are required to update head, tail and size as required. 5. Input and Output is managed for you. Just update the code in removeLast function. Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't force you but the intention is to teach a concept. Play in spirit of the question.

{"id":"70c0b04c-987d-4a4c-bf4e-17c4e4fa5ac0","name":"Remove Last In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of removeLast function. This function is supposed to add an element to the front of LinkedList. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\". \r\n4. You are required to update head, tail and size as required.\r\n5. Input and Output is managed for you. Just update the code in removeLast function.\r\n\r\nNote -> Use the code snippet and follow the algorithm discussed in question video. The judge can't \r\n force you but the intention is to teach a concept. Play in spirit of the question.\r\n","inputFormat":"input in managed for you.\r\n","outputFormat":"output in managed for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include<iostream>\nusing namespace std;\n#include<vector>\n\n\nclass DoublyLinkedList {\n public : class Node {\n public:\n int data = 0;\n Node* prev = nullptr;\n Node* next = nullptr;\n\n Node(int data) {\n this->data = data;\n }\n\n };\n\n Node* head = nullptr;\n Node* tail = nullptr;\n int size = 0;\n\n string toString() {\n string sb;\n Node* curr = this->head;\n sb += \"[\";\n while (curr != nullptr) {\n sb += (to_string)(curr->data);\n if (curr->next != nullptr)\n sb+=\", \";\n curr = curr->next;\n }\n sb += \"]\";\n\n return sb;\n }\n\n // Exceptions========================================\n\n private:bool ListIsEmptyException() {\n if (this->size == 0) {\n cout << \"ListIsEmpty: \";\n return true;\n }\n return false;\n }\n // BasicFunctions======================================\n\n public:int Size() {\n // write your code here\n return size;\n }\n\n public:bool isEmpty() {\n // write your code here\n return size==0;\n }\n\n // AddFunctions======================================\n\n public:void addFirstNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n node->next = this->head;\n this->head->prev = node;\n this->head = node;\n }\n this->size++;\n }\n\n public: void addFirst(int val) {\n Node* node = new Node(val);\n addFirstNode(node);\n }\n\n public:void addLastNode(Node* node) {\n if (this->size == 0)\n this->head = this->tail = node;\n else {\n this->tail->next = node;\n node->prev = this->tail;\n this->tail = node;\n }\n this->size++;\n }\n\n public:void addLast(int val) {\n Node* node = new Node(val);\n addLastNode(node);\n }\n\n // RemoveFunctions======================================\n\n public:Node *removeFirstNode() {\n Node* node = this->head;\n if (this->size == 1)\n this->head = this->tail = nullptr;\n else {\n Node* nextNode = this->head->next;\n nextNode->prev = nullptr;\n node->next = nullptr;\n\n this->head = nextNode;\n }\n\n this->size--;\n return node;\n }\n\n public:int removeFirst() {\n if (ListIsEmptyException())\n return -1;\n Node* node = removeFirstNode();\n return node->data;\n }\n\n public:Node* removeLastNode() {\n Node* node = this->tail;\n if (this->size == 1)\n this->head = this->tail = nullptr;\n else {\n Node* prevNode = this->tail->prev;\n prevNode->next = nullptr;\n node->prev = nullptr;\n\n this->tail = prevNode;\n }\n\n this->size--;\n return node;\n }\n\n\n public:int removeLast() {\n if(ListIsEmptyException()){\n return -1 ;\n }\n Node *node = removeLastNode() ;\n return node->data ;\n }\n\n\n };\n \n\n int main() {\n DoublyLinkedList *dll = new DoublyLinkedList();\n \n string str;\n cin >> str;\n \n while(str != \"stop\"){\n int data;\n if(str == (\"addFirst\")){\n cin >> data;\n dll->addFirst(data);\n }\n else if (str == (\"addLast\")){\n cin >> data;\n dll->addLast((data));\n }\n else if (str == (\"removeFirst\"))\n cout << (dll->removeFirst()) << endl;\n else if (str == (\"removeLast\"))\n cout << (dll->removeLast()) << endl;\n cin >> str;\n }\n cout << dll->toString();\n \n return 0; \n }"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n\r\n public static class DoublyLinkedList {\r\n private class Node {\r\n int data = 0;\r\n Node prev = null;\r\n Node next = null;\r\n\r\n Node(int data) {\r\n this.data = data;\r\n }\r\n\r\n }\r\n\r\n private Node head = null;\r\n private Node tail = null;\r\n private int size = 0;\r\n\r\n public String toString() {\r\n StringBuilder sb = new StringBuilder();\r\n Node curr = this.head;\r\n sb.append(\"[\");\r\n while (curr != null) {\r\n sb.append(curr.data);\r\n if (curr.next != null)\r\n sb.append(\", \");\r\n curr = curr.next;\r\n }\r\n sb.append(\"]\");\r\n\r\n return sb.toString();\r\n }\r\n\r\n // Exceptions========================================\r\n\r\n private boolean ListIsEmptyException() {\r\n if (this.size == 0) {\r\n System.out.print(\"ListIsEmpty: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // AddFunctions======================================\r\n\r\n private void addFirstNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n node.next = this.head;\r\n this.head.prev = node;\r\n this.head = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addFirst(int val) {\r\n Node node = new Node(val);\r\n addFirstNode(node);\r\n }\r\n\r\n private void addLastNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n this.tail.next = node;\r\n node.prev = this.tail;\r\n this.tail = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addLast(int val) {\r\n Node node = new Node(val);\r\n addLastNode(node);\r\n }\r\n\r\n // RemoveFunctions======================================\r\n\r\n private Node removeFirstNode() {\r\n Node node = this.head;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node nextNode = this.head.next;\r\n nextNode.prev = null;\r\n node.next = null;\r\n\r\n this.head = nextNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeFirstNode();\r\n return node.data;\r\n }\r\n\r\n public int removeLast() {\r\n\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n DoublyLinkedList dll = new DoublyLinkedList();\r\n\r\n String str = scn.nextLine();\r\n while (!str.equals(\"stop\")) {\r\n String[] s = str.split(\" \");\r\n if (s[0].equals(\"addFirst\"))\r\n dll.addFirst(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"addLast\"))\r\n dll.addLast(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"removeFirst\"))\r\n System.out.println(dll.removeFirst());\r\n else if (s[0].equals(\"removeLast\"))\r\n System.out.println(dll.removeLast());\r\n str = scn.nextLine();\r\n }\r\n\r\n System.out.println(dll);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"addFirst 4\r\naddFirst 4\r\naddLast 5\r\naddFirst 7\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveLast\r\nremoveFirst\r\nremoveFirst\r\nstop","sampleOutput":"7\r\n4\r\n4\r\n1\r\n5\r\nListIsEmpty: -1\r\n[]\r\n","questionVideo":"https://www.youtube.com/embed/iqe6qrKTyAc","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":"02c37ec4-3b5b-4b39-96ae-1c3b6c1ca9bf","name":"Remove Last In Doubly Linkedlist","slug":"remove-last-in-doubly-linkedlist","type":1}],"next":{"id":"e8a09549-5527-4377-bd4d-5c8502d8ff3d","name":"Remove Last In Doubly Linked List","type":3,"slug":"remove-last-in-doubly-linked-list"},"prev":{"id":"57f19c2e-b2b6-4e0d-874f-f70a077d0c37","name":"Remove First In Doubly Linked List","type":3,"slug":"remove-first-in-doubly-linked-list"}}}
plane

Editor


Loading...

Remove Last In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of removeLast function. This function is supposed to add an element to the front of LinkedList. 3. If size of list is zero then return "ListIsEmpty: -1". 4. You are required to update head, tail and size as required. 5. Input and Output is managed for you. Just update the code in removeLast function. Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't force you but the intention is to teach a concept. Play in spirit of the question.

Constraints

0 <= N <= 10^6

Format

Input

input in managed for you.

Output

output in managed for you.

Example

Sample Input

addFirst 4 addFirst 4 addLast 5 addFirst 7 addLast 1 removeFirst removeFirst removeFirst removeLast removeFirst removeFirst stop

Sample Output

7 4 4 1 5 ListIsEmpty: -1 []

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode