{"id":"a81692c5-0b95-414d-a3a3-4b3421aaac83","name":"Add Last In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of addLast 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 addLast 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\nprivate:\r\n void addFirstNode(Node* node)\r\n {\r\n if (this->size == 0)\r\n this->head = this->tail = node;\r\n else\r\n {\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\npublic:\r\n void addFirst(int data)\r\n {\r\n Node* node = new Node(data);\r\n addFirstNode(node);\r\n }\r\n\r\n void addLast(int data)\r\n {\r\n // complete this function.\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 else if (word == \"addLast\")\r\n {\r\n int data;\r\n ss >> data;\r\n dll.addLast(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 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 public void addLast(int val) {\r\n // complete this function.\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\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\nstop","sampleOutput":"[7, 4, 4, 5, 1]\r\n","questionVideo":"https://www.youtube.com/embed/bMXgF_tUMHE","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":"3def24f9-049d-4f69-8d7c-f0dbfc648b52","name":"Add Last In Doubly Linkedlist","slug":"add-last-in-doubly-linkedlist","type":1}],"next":{"id":"3e0f42aa-ae53-4d77-8f9f-dd693d00e69d","name":"Get At In Doubly Linkedlist","type":3,"slug":"get-at-in-doubly-linkedlist"},"prev":{"id":"a6b58846-d5c7-4d10-b2a1-5382b06ae8e9","name":"Add First In Doubly Linked List","type":3,"slug":"add-first-in-doubly-linked-list"}}}

Add Last In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of addLast 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 addLast 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":"a81692c5-0b95-414d-a3a3-4b3421aaac83","name":"Add Last In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of addLast 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 addLast 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\nprivate:\r\n void addFirstNode(Node* node)\r\n {\r\n if (this->size == 0)\r\n this->head = this->tail = node;\r\n else\r\n {\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\npublic:\r\n void addFirst(int data)\r\n {\r\n Node* node = new Node(data);\r\n addFirstNode(node);\r\n }\r\n\r\n void addLast(int data)\r\n {\r\n // complete this function.\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 else if (word == \"addLast\")\r\n {\r\n int data;\r\n ss >> data;\r\n dll.addLast(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 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 public void addLast(int val) {\r\n // complete this function.\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\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\nstop","sampleOutput":"[7, 4, 4, 5, 1]\r\n","questionVideo":"https://www.youtube.com/embed/bMXgF_tUMHE","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":"3def24f9-049d-4f69-8d7c-f0dbfc648b52","name":"Add Last In Doubly Linkedlist","slug":"add-last-in-doubly-linkedlist","type":1}],"next":{"id":"3e0f42aa-ae53-4d77-8f9f-dd693d00e69d","name":"Get At In Doubly Linkedlist","type":3,"slug":"get-at-in-doubly-linkedlist"},"prev":{"id":"a6b58846-d5c7-4d10-b2a1-5382b06ae8e9","name":"Add First In Doubly Linked List","type":3,"slug":"add-first-in-doubly-linked-list"}}}
plane

Editor


Loading...

Add Last In Doubly Linkedlist

easy

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of addLast 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 addLast 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 stop

Sample Output

[7, 4, 4, 5, 1]

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode