{"id":"4e63f055-b66a-4c2b-878c-d43c7d1e34dd","name":"Add First In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of addFirst function. This function is supposed to add an element to the front of LinkedList. You are required to update head, tail and size as required.\r\n3. Input and Output is managed for you. Just update the code in addFirst 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 <bits/stdc++.h>\r\nusing namespace std;\r\n\r\nclass DoublyLinkedList\r\n{\r\n class Node\r\n {\r\n public:\r\n int data = 0;\r\n Node* prev = nullptr;\r\n Node* next = nullptr;\r\n\r\n Node(int data)\r\n {\r\n this->data = data;\r\n }\r\n };\r\n\r\n Node* head = nullptr;\r\n Node* tail = nullptr;\r\n int size = 0;\r\n\r\npublic:\r\n void display()\r\n {\r\n string str = \"\";\r\n Node* curr = this->head;\r\n str += \"[\";\r\n while (curr != nullptr)\r\n {\r\n str += to_string(curr->data);\r\n if (curr->next != nullptr)\r\n str += \", \";\r\n curr = curr->next;\r\n }\r\n str += \"]\";\r\n\r\n cout << str << endl;\r\n }\r\n\r\n void addFirst(int data)\r\n {\r\n }\r\n};\r\n\r\nint main()\r\n{\r\n DoublyLinkedList dll;\r\n string s;\r\n while (getline(cin, s))\r\n {\r\n stringstream ss(s);\r\n string word;\r\n ss >> word;\r\n if (word == \"stop\")\r\n break;\r\n if (word == \"addFirst\")\r\n {\r\n int data;\r\n ss >> data;\r\n dll.addFirst(data);\r\n }\r\n }\r\n\r\n dll.display();\r\n\r\n return 0;\r\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 public void addFirst(int val) {\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\r\n str = scn.nextLine();\r\n }\r\n System.out.println(dll);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"addFirst 10\r\naddFirst 20\r\naddFirst 30\r\nstop","sampleOutput":"[30, 20, 10]","questionVideo":"https://www.youtube.com/embed/3awBtfh1eAo","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":"c5fffeb5-1963-425e-881f-3c1d2d57a054","name":"Add First In Doubly Linkedlist","slug":"add-first-in-doubly-linkedlist","type":1}],"next":{"id":"68c099bc-c97b-433e-aadf-fc4645e2b63a","name":"Add First In Doubly Linked List MCQ","type":0,"slug":"add-first-in-doubly-linked-list-mcq"},"prev":{"id":"a2fca537-b4c0-4f64-8db5-53826b547863","name":"Quicksort In Linkedlist","type":1,"slug":"quicksort-in-linkedlist"}}}

Add First In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of addFirst function. This function is supposed to add an element to the front of LinkedList. You are required to update head, tail and size as required. 3. Input and Output is managed for you. Just update the code in addFirst 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":"4e63f055-b66a-4c2b-878c-d43c7d1e34dd","name":"Add First In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of addFirst function. This function is supposed to add an element to the front of LinkedList. You are required to update head, tail and size as required.\r\n3. Input and Output is managed for you. Just update the code in addFirst 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 <bits/stdc++.h>\r\nusing namespace std;\r\n\r\nclass DoublyLinkedList\r\n{\r\n class Node\r\n {\r\n public:\r\n int data = 0;\r\n Node* prev = nullptr;\r\n Node* next = nullptr;\r\n\r\n Node(int data)\r\n {\r\n this->data = data;\r\n }\r\n };\r\n\r\n Node* head = nullptr;\r\n Node* tail = nullptr;\r\n int size = 0;\r\n\r\npublic:\r\n void display()\r\n {\r\n string str = \"\";\r\n Node* curr = this->head;\r\n str += \"[\";\r\n while (curr != nullptr)\r\n {\r\n str += to_string(curr->data);\r\n if (curr->next != nullptr)\r\n str += \", \";\r\n curr = curr->next;\r\n }\r\n str += \"]\";\r\n\r\n cout << str << endl;\r\n }\r\n\r\n void addFirst(int data)\r\n {\r\n }\r\n};\r\n\r\nint main()\r\n{\r\n DoublyLinkedList dll;\r\n string s;\r\n while (getline(cin, s))\r\n {\r\n stringstream ss(s);\r\n string word;\r\n ss >> word;\r\n if (word == \"stop\")\r\n break;\r\n if (word == \"addFirst\")\r\n {\r\n int data;\r\n ss >> data;\r\n dll.addFirst(data);\r\n }\r\n }\r\n\r\n dll.display();\r\n\r\n return 0;\r\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 public void addFirst(int val) {\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\r\n str = scn.nextLine();\r\n }\r\n System.out.println(dll);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"addFirst 10\r\naddFirst 20\r\naddFirst 30\r\nstop","sampleOutput":"[30, 20, 10]","questionVideo":"https://www.youtube.com/embed/3awBtfh1eAo","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":"c5fffeb5-1963-425e-881f-3c1d2d57a054","name":"Add First In Doubly Linkedlist","slug":"add-first-in-doubly-linkedlist","type":1}],"next":{"id":"68c099bc-c97b-433e-aadf-fc4645e2b63a","name":"Add First In Doubly Linked List MCQ","type":0,"slug":"add-first-in-doubly-linked-list-mcq"},"prev":{"id":"a2fca537-b4c0-4f64-8db5-53826b547863","name":"Quicksort In Linkedlist","type":1,"slug":"quicksort-in-linkedlist"}}}
plane

Editor


Loading...

Add First In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of addFirst function. This function is supposed to add an element to the front of LinkedList. You are required to update head, tail and size as required. 3. Input and Output is managed for you. Just update the code in addFirst 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 10 addFirst 20 addFirst 30 stop

Sample Output

[30, 20, 10]

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode