{"id":"f439120d-3570-4091-8b65-7f5fe08ea383","name":"Remove At In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of RemoveAt function. This function is supposed to Remove value at given index and return in back. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\".\r\n4. If Index is Invalid then return \"IndexIsInValid: -1\". \r\n5. You are required to update head, tail and size as required.\r\n6. Input and Output is managed for you. Just update the code in incomplete 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 private:bool indexIsInvalidException(int index,int leftrange,int rightrange){\n if(index < leftrange || index > rightrange ){\n cout<<\"IndexIsInValid: \";\n return true;\n }\n return false;\n } \n\n // BasicFunctions======================================\n\n public:int Size() {\n \n return size;\n }\n\n public:bool isEmpty() {\n \n return size==0;\n }\n // getFunctions======================================\n public:int getFirst() {\n if(ListIsEmptyException()){\n return -1;\n }\n return head->data;\n \n }\n\n public:int getLast() {\n \n if(ListIsEmptyException()){\n return -1;\n }\n return tail->data;\n }\n \n public : Node* getNodeAt(int index){\n Node *curr=this->head;\n while(index-- > 0){\n curr=curr->next;\n }\n return curr;\n \n } \n public : int getAt(int index){\n if(ListIsEmptyException())return -1;\n else if(indexIsInvalidException(index, 0, this->size - 1)){\n return -1;\n }\n Node *node=getNodeAt(index);\n return node->data;\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 public: void addNodeAt(int index,Node *node){\n if(index==0)addFirstNode(node);\n else if(index==this->size)addLastNode(node);\n else{\n Node *frwrd=getNodeAt(index);\n Node *prev=frwrd->prev;\n\n prev->next=node;\n node->prev=prev;\n\n\n node->next=frwrd;\n frwrd->prev=node;\n\n this->size++;\n }\n }\n public: void addAt(int index,int data){\n if(indexIsInvalidException(index,0,this->size)){\n cout<<-1<<endl;\n }else{\n Node *node=new Node(data);\n addNodeAt(index,node);\n }\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 public:int removeLast() {\n if (ListIsEmptyException())\n return -1;\n Node* node = removeLastNode();\n return node->data;\n }\n\n public: int removeAt(int index) {\n \n //write your code here\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,index;\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 else if (str == (\"getFirst\"))\n cout << (dll->getFirst()) << endl;\n else if (str == (\"getLast\"))\n cout << (dll->getLast()) << endl;\n else if (str == (\"size\"))\n cout << (dll->Size()) << endl;\n else if (str == (\"isEmpty\")){\n if((dll->isEmpty())){\n cout<<\"true\"<<\"\\n\";\n } else cout<<\"false\"<< endl;\n }\n \n else if(str == (\"getAt\")){\n cin >> data;\n cout << dll->getAt(data) << endl ;\n }\n else if(str == (\"addAt\")){\n cin>>index;cin>>data;\n dll->addAt(index,data);\n }\n else if(str == (\"removeAt\")){\n cin>>data;\n dll->removeAt(data);\n }\n \n cin >> str;\n }\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 private boolean indexIsInvalidException(int index, int leftRange, int rightRange) {\r\n if (index < leftRange || index > rightRange) {\r\n System.out.print(\"IndexIsInValid: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // BasicFunctions======================================\r\n\r\n public int size() {\r\n return this.size;\r\n }\r\n\r\n public boolean isEmpty() {\r\n return this.size == 0;\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 private void addNodeAt(int index, Node node) {\r\n if (index == 0)\r\n addFirstNode(node);\r\n else if (index == this.size)\r\n addLastNode(node);\r\n else {\r\n Node forw = getNodeAt(index);\r\n Node prev = forw.prev;\r\n\r\n prev.next = node;\r\n node.prev = prev;\r\n\r\n node.next = forw;\r\n forw.prev = node;\r\n\r\n this.size++;\r\n }\r\n }\r\n\r\n public void addAt(int index, int data) {\r\n if (indexIsInvalidException(index, 0, this.size))\r\n System.out.println(-1);\r\n else {\r\n Node node = new Node(data);\r\n addNodeAt(index, node);\r\n }\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 private Node removeLastNode() {\r\n Node node = this.tail;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node prevNode = this.tail.prev;\r\n prevNode.next = null;\r\n node.prev = null;\r\n\r\n this.tail = prevNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeLastNode();\r\n return node.data;\r\n }\r\n\r\n public int removeAt(int index) {\r\n return -1;\r\n }\r\n\r\n // getFunctions======================================\r\n\r\n public int getFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.head.data;\r\n }\r\n\r\n public int getLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.tail.data;\r\n }\r\n\r\n private Node getNodeAt(int index) {\r\n Node curr = this.head;\r\n while (index-- > 0)\r\n curr = curr.next;\r\n\r\n return curr;\r\n }\r\n\r\n public int getAt(int index) {\r\n if (ListIsEmptyException())\r\n return -1;\r\n else if (indexIsInvalidException(index, 0, this.size - 1))\r\n return -1;\r\n\r\n Node node = getNodeAt(index);\r\n return node.data;\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 else if (s[0].equals(\"getFirst\"))\r\n System.out.println(dll.getFirst());\r\n else if (s[0].equals(\"getLast\"))\r\n System.out.println(dll.getLast());\r\n else if (s[0].equals(\"size\"))\r\n System.out.println(dll.size());\r\n else if (s[0].equals(\"isEmpty\"))\r\n System.out.println(dll.isEmpty());\r\n else if (s[0].equals(\"getAt\"))\r\n System.out.println(dll.getAt(Integer.parseInt(s[1])));\r\n else if (s[0].equals(\"addAt\"))\r\n dll.addAt(Integer.parseInt(s[1]), Integer.parseInt(s[2]));\r\n else if (s[0].equals(\"removeAt\"))\r\n dll.removeAt(Integer.parseInt(s[1]));\r\n\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\ngetAt 4 3\r\naddAt 4 34\r\naddAt 0 43\r\nremoveAt 4\r\nremoveAt 0\r\naddAt 8 545\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nsize\r\nisEmpty\r\ngetFirst\r\nremoveFirst\r\nremoveLast\r\ngetLast\r\nremoveFirst\r\nremoveFirst\r\naddAt 0 345\r\nstop","sampleOutput":"IndexIsInValid: -1\r\nIndexIsInValid: -1\r\n7\r\n4\r\n3\r\nfalse\r\n4\r\n4\r\n1\r\n34\r\n34\r\nListIsEmpty: -1\r\n[345]\r\n","questionVideo":"https://www.youtube.com/embed/AIC5d4gWFH4","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":"3275e076-e676-4cb9-a955-e70e71541bc9","name":"Remove At In Doubly Linkedlist","slug":"remove-at-in-doubly-linkedlist","type":1}],"next":{"id":"3ac162d6-d753-45b0-a496-1b359ee00525","name":"Remove At In Doubly Linkedlist Medium MCQ","type":0,"slug":"remove-at-in-doubly-linkedlist-medium-mcq"},"prev":{"id":"0e99a3b8-72bf-405a-80b0-31d1a32c4798","name":"Add at in doubly linked list","type":3,"slug":"add-at-in-doubly-linked-list"}}}

Remove At In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of RemoveAt function. This function is supposed to Remove value at given index and return in back. 3. If size of list is zero then return "ListIsEmpty: -1". 4. If Index is Invalid then return "IndexIsInValid: -1". 5. You are required to update head, tail and size as required. 6. Input and Output is managed for you. Just update the code in incomplete 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":"f439120d-3570-4091-8b65-7f5fe08ea383","name":"Remove At In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of RemoveAt function. This function is supposed to Remove value at given index and return in back. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\".\r\n4. If Index is Invalid then return \"IndexIsInValid: -1\". \r\n5. You are required to update head, tail and size as required.\r\n6. Input and Output is managed for you. Just update the code in incomplete 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 private:bool indexIsInvalidException(int index,int leftrange,int rightrange){\n if(index < leftrange || index > rightrange ){\n cout<<\"IndexIsInValid: \";\n return true;\n }\n return false;\n } \n\n // BasicFunctions======================================\n\n public:int Size() {\n \n return size;\n }\n\n public:bool isEmpty() {\n \n return size==0;\n }\n // getFunctions======================================\n public:int getFirst() {\n if(ListIsEmptyException()){\n return -1;\n }\n return head->data;\n \n }\n\n public:int getLast() {\n \n if(ListIsEmptyException()){\n return -1;\n }\n return tail->data;\n }\n \n public : Node* getNodeAt(int index){\n Node *curr=this->head;\n while(index-- > 0){\n curr=curr->next;\n }\n return curr;\n \n } \n public : int getAt(int index){\n if(ListIsEmptyException())return -1;\n else if(indexIsInvalidException(index, 0, this->size - 1)){\n return -1;\n }\n Node *node=getNodeAt(index);\n return node->data;\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 public: void addNodeAt(int index,Node *node){\n if(index==0)addFirstNode(node);\n else if(index==this->size)addLastNode(node);\n else{\n Node *frwrd=getNodeAt(index);\n Node *prev=frwrd->prev;\n\n prev->next=node;\n node->prev=prev;\n\n\n node->next=frwrd;\n frwrd->prev=node;\n\n this->size++;\n }\n }\n public: void addAt(int index,int data){\n if(indexIsInvalidException(index,0,this->size)){\n cout<<-1<<endl;\n }else{\n Node *node=new Node(data);\n addNodeAt(index,node);\n }\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 public:int removeLast() {\n if (ListIsEmptyException())\n return -1;\n Node* node = removeLastNode();\n return node->data;\n }\n\n public: int removeAt(int index) {\n \n //write your code here\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,index;\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 else if (str == (\"getFirst\"))\n cout << (dll->getFirst()) << endl;\n else if (str == (\"getLast\"))\n cout << (dll->getLast()) << endl;\n else if (str == (\"size\"))\n cout << (dll->Size()) << endl;\n else if (str == (\"isEmpty\")){\n if((dll->isEmpty())){\n cout<<\"true\"<<\"\\n\";\n } else cout<<\"false\"<< endl;\n }\n \n else if(str == (\"getAt\")){\n cin >> data;\n cout << dll->getAt(data) << endl ;\n }\n else if(str == (\"addAt\")){\n cin>>index;cin>>data;\n dll->addAt(index,data);\n }\n else if(str == (\"removeAt\")){\n cin>>data;\n dll->removeAt(data);\n }\n \n cin >> str;\n }\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 private boolean indexIsInvalidException(int index, int leftRange, int rightRange) {\r\n if (index < leftRange || index > rightRange) {\r\n System.out.print(\"IndexIsInValid: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // BasicFunctions======================================\r\n\r\n public int size() {\r\n return this.size;\r\n }\r\n\r\n public boolean isEmpty() {\r\n return this.size == 0;\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 private void addNodeAt(int index, Node node) {\r\n if (index == 0)\r\n addFirstNode(node);\r\n else if (index == this.size)\r\n addLastNode(node);\r\n else {\r\n Node forw = getNodeAt(index);\r\n Node prev = forw.prev;\r\n\r\n prev.next = node;\r\n node.prev = prev;\r\n\r\n node.next = forw;\r\n forw.prev = node;\r\n\r\n this.size++;\r\n }\r\n }\r\n\r\n public void addAt(int index, int data) {\r\n if (indexIsInvalidException(index, 0, this.size))\r\n System.out.println(-1);\r\n else {\r\n Node node = new Node(data);\r\n addNodeAt(index, node);\r\n }\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 private Node removeLastNode() {\r\n Node node = this.tail;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node prevNode = this.tail.prev;\r\n prevNode.next = null;\r\n node.prev = null;\r\n\r\n this.tail = prevNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeLastNode();\r\n return node.data;\r\n }\r\n\r\n public int removeAt(int index) {\r\n return -1;\r\n }\r\n\r\n // getFunctions======================================\r\n\r\n public int getFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.head.data;\r\n }\r\n\r\n public int getLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.tail.data;\r\n }\r\n\r\n private Node getNodeAt(int index) {\r\n Node curr = this.head;\r\n while (index-- > 0)\r\n curr = curr.next;\r\n\r\n return curr;\r\n }\r\n\r\n public int getAt(int index) {\r\n if (ListIsEmptyException())\r\n return -1;\r\n else if (indexIsInvalidException(index, 0, this.size - 1))\r\n return -1;\r\n\r\n Node node = getNodeAt(index);\r\n return node.data;\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 else if (s[0].equals(\"getFirst\"))\r\n System.out.println(dll.getFirst());\r\n else if (s[0].equals(\"getLast\"))\r\n System.out.println(dll.getLast());\r\n else if (s[0].equals(\"size\"))\r\n System.out.println(dll.size());\r\n else if (s[0].equals(\"isEmpty\"))\r\n System.out.println(dll.isEmpty());\r\n else if (s[0].equals(\"getAt\"))\r\n System.out.println(dll.getAt(Integer.parseInt(s[1])));\r\n else if (s[0].equals(\"addAt\"))\r\n dll.addAt(Integer.parseInt(s[1]), Integer.parseInt(s[2]));\r\n else if (s[0].equals(\"removeAt\"))\r\n dll.removeAt(Integer.parseInt(s[1]));\r\n\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\ngetAt 4 3\r\naddAt 4 34\r\naddAt 0 43\r\nremoveAt 4\r\nremoveAt 0\r\naddAt 8 545\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nsize\r\nisEmpty\r\ngetFirst\r\nremoveFirst\r\nremoveLast\r\ngetLast\r\nremoveFirst\r\nremoveFirst\r\naddAt 0 345\r\nstop","sampleOutput":"IndexIsInValid: -1\r\nIndexIsInValid: -1\r\n7\r\n4\r\n3\r\nfalse\r\n4\r\n4\r\n1\r\n34\r\n34\r\nListIsEmpty: -1\r\n[345]\r\n","questionVideo":"https://www.youtube.com/embed/AIC5d4gWFH4","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":"3275e076-e676-4cb9-a955-e70e71541bc9","name":"Remove At In Doubly Linkedlist","slug":"remove-at-in-doubly-linkedlist","type":1}],"next":{"id":"3ac162d6-d753-45b0-a496-1b359ee00525","name":"Remove At In Doubly Linkedlist Medium MCQ","type":0,"slug":"remove-at-in-doubly-linkedlist-medium-mcq"},"prev":{"id":"0e99a3b8-72bf-405a-80b0-31d1a32c4798","name":"Add at in doubly linked list","type":3,"slug":"add-at-in-doubly-linked-list"}}}
plane

Editor


Loading...

Remove At In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of RemoveAt function. This function is supposed to Remove value at given index and return in back. 3. If size of list is zero then return "ListIsEmpty: -1". 4. If Index is Invalid then return "IndexIsInValid: -1". 5. You are required to update head, tail and size as required. 6. Input and Output is managed for you. Just update the code in incomplete 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 getAt 4 3 addAt 4 34 addAt 0 43 removeAt 4 removeAt 0 addAt 8 545 addLast 1 removeFirst removeFirst size isEmpty getFirst removeFirst removeLast getLast removeFirst removeFirst addAt 0 345 stop

Sample Output

IndexIsInValid: -1 IndexIsInValid: -1 7 4 3 false 4 4 1 34 34 ListIsEmpty: -1 [345]

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode