{"id":"c2c7162e-8b51-41ea-833f-f06b0afbafe7","name":"Remove First In Linkedlist","description":"1. You are given a partially written LinkedList class.\r\n2. Here is a list of existing functions:\r\n 2.1 addLast - adds a new element with given value to the end of Linked List\r\n 2.2. display - Prints the elements of linked list from front to end in a single line. All \r\n elements are separated by space\r\n 2.3. size - Returns the number of elements in the linked list.\r\n3. You are required to complete the body of removeFirst function \r\n 3.1. removeFirst - This function is required to remove the first element from \r\n Linked List. Also, if there is only one element, this should set head and tail to \r\n null. If there are no elements, this should print \"List is empty\".\r\n4. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include<string>\nusing namespace std;\n\nclass node\n{\npublic :\n int data;\n node* next;\n};\n\nclass linked_list\n{\npublic:\n node* head, *tail;\n int size = 0;\n\npublic:\n linked_list()\n {\n head = NULL;\n tail = NULL;\n }\n\n void addLast(int n)\n {\n node* tmp = new node;\n tmp->data = n;\n tmp->next = NULL;\n\n if (head == NULL)\n {\n head = tmp;\n tail = tmp;\n }\n else\n {\n tail->next = tmp;\n tail = tail->next;\n }\n size++;\n\n\n }\n void display() {\n for (node* tmp = head; tmp != NULL; tmp = tmp->next) {\n cout << tmp->data << \" \";\n }\n cout << endl;\n\n }\n void removeFirst() {\n //write your code here\n }\n\n\n\n void testList() {\n for (node* temp = head; temp != NULL; temp = temp->next) {\n cout << temp->data << endl;\n }\n cout << size << endl;\n\n if (size > 0) {\n cout << tail->data << endl;\n }\n }\n};\n\n\nint main() {\n\n\n string str;\n linked_list l;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'a') {\n string ss = str.substr(8, 2);\n int n = stoi(ss);\n l.addLast( n);\n\n }\n else if (str[0] == 's') {\n cout << l.size << endl;\n }\n else if (str[0] == 'd') {\n l.display();\n }\n else if (str[0] == 'r') {\n l.removeFirst();\n }\n }\n\n\n return 0;\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node next;\r\n }\r\n\r\n public static class LinkedList {\r\n Node head;\r\n Node tail;\r\n int size;\r\n\r\n void addLast(int val) {\r\n Node temp = new Node();\r\n temp.data = val;\r\n temp.next = null;\r\n\r\n if (size == 0) {\r\n head = tail = temp;\r\n } else {\r\n tail.next = temp;\r\n tail = temp;\r\n }\r\n\r\n size++;\r\n }\r\n\r\n public int size(){\r\n return size;\r\n }\r\n\r\n public void display(){\r\n for(Node temp = head; temp != null; temp = temp.next){\r\n System.out.print(temp.data + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n public void removeFirst(){\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n LinkedList list = new LinkedList();\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"addLast\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n list.addLast(val);\r\n } else if(str.startsWith(\"size\")){\r\n System.out.println(list.size());\r\n } else if(str.startsWith(\"display\")){\r\n list.display();\r\n } else if(str.startsWith(\"removeFirst\")){\r\n list.removeFirst();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n def __init__(self, data=None):\n self.data = data\n self.next = None\nclass SLinkedList:\n def __init__(self):\n self.head = None\n self.size=0\n self.tail=None\n\n def AddLast(self, newdata):\n NewNode = Node(newdata)\n if self.head is None:\n self.head = NewNode\n self.tail = NewNode\n self.size+=1\n return\n laste = self.head\n while(laste.next):\n laste = laste.next\n laste.next=NewNode\n self.tail=laste.next \n self.size+=1\n\n def display(self):\n printval = self.head\n while printval is not None:\n print (printval.data,end=\" \")\n printval = printval.next\n print(\" \")\n def testList(self) :\n temp = self.head\n while temp is not None: \n print(temp.data)\n temp = temp.next\n \n print(self.size)\n\n if self.size > 0 :\n \n print(self.tail.data)\n \n \n def removeFirst(self) :\n # write your code here\n\n\n\nl1 = SLinkedList()\nwhile 1>0 :\n str=input()\n\n if str[0]=='q':\n \n break;\n\n elif str[0]=='a':\n \n val=int(str[-3]+str[-2])\n \n l1.AddLast(val)\n \n elif str[0]=='d':\n \n\n l1.display() \n \n elif str[0]=='s':\n \n\n print(l1.size) \n \n elif str[0]=='r':\n \n\n l1.removeFirst()"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\naddLast 20\r\naddLast 30\r\ndisplay\r\nremoveFirst\r\nsize\r\naddLast 40\r\naddLast 50\r\nremoveFirst\r\ndisplay\r\nsize\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nquit","sampleOutput":"10 20 30 \r\n2\r\n30 40 50 \r\n3\r\nList is empty","questionVideo":"https://www.youtube.com/embed/3za9tOwFyCY","hints":[],"associated":[{"id":"238a1b16-5884-4818-889b-a9838a2dfa1d","name":"The two parts of node in singly linked list are :-(Q- Remove First In LinkedList)","slug":"the-two-parts-of-node-in-singly-linked-list-are-q-remove-first-in-linkedlist","type":4},{"id":"60647db9-fda9-4d3a-a8bc-d39c2583d7d9","name":"If the size of the linked list is one, which of the following is correct way to implement to remove the first node?(Q- Remove First In LinkedList)","slug":"if-the-size-of-the-linked-list-is-one-which-of-the-following-is-correct-way-to-implement-to-remove-the-first-node-q-remove-first-in-linkedlist","type":4},{"id":"6f79e3d7-f921-4dad-add8-9c6f5a6f8e90","name":"Which of the following is/are being updated after removeing the first node to the linked list if the list is not empty?(q - Remove First In LinkedList)","slug":"which-of-the-following-is-are-being-updated-after-removeing-the-first-node-to-the-linked-list-if-the-list-is-not-empty-q-remove-first-in-linkedlist","type":4},{"id":"b911179d-ae61-44bd-b235-94f1d7f39337","name":"What would be the time complexity of this problem?(Q- Remove First In LinkedList)","slug":"what-would-be-the-time-complexity-of-this-problem-q-remove-first-in-linkedlist","type":4}],"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":"91027ef1-2784-45bf-8143-cc6af4560105","name":"Linked Lists For Beginners","slug":"linked-lists-for-beginners","type":0},{"id":"f9abd6f3-2797-4f00-bdf1-829cd76f72b1","name":"Remove First In Linkedlist","slug":"remove-first-in-linkedlist","type":1}],"next":{"id":"fea21342-951a-48d9-b496-50141dcd1e62","name":"Remove First In LinkedList","type":3,"slug":"remove-first-in-linkedlist"},"prev":{"id":"8a25a8e6-6fb7-42f1-9217-7d0f578dee2b","name":"Display a Linked List","type":3,"slug":"display-a-linked-list"}}}

Remove First In Linkedlist

1. You are given a partially written LinkedList class. 2. Here is a list of existing functions: 2.1 addLast - adds a new element with given value to the end of Linked List 2.2. display - Prints the elements of linked list from front to end in a single line. All elements are separated by space 2.3. size - Returns the number of elements in the linked list. 3. You are required to complete the body of removeFirst function 3.1. removeFirst - This function is required to remove the first element from Linked List. Also, if there is only one element, this should set head and tail to null. If there are no elements, this should print "List is empty". 4. Input and Output is managed for you.

{"id":"c2c7162e-8b51-41ea-833f-f06b0afbafe7","name":"Remove First In Linkedlist","description":"1. You are given a partially written LinkedList class.\r\n2. Here is a list of existing functions:\r\n 2.1 addLast - adds a new element with given value to the end of Linked List\r\n 2.2. display - Prints the elements of linked list from front to end in a single line. All \r\n elements are separated by space\r\n 2.3. size - Returns the number of elements in the linked list.\r\n3. You are required to complete the body of removeFirst function \r\n 3.1. removeFirst - This function is required to remove the first element from \r\n Linked List. Also, if there is only one element, this should set head and tail to \r\n null. If there are no elements, this should print \"List is empty\".\r\n4. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n#include<string>\nusing namespace std;\n\nclass node\n{\npublic :\n int data;\n node* next;\n};\n\nclass linked_list\n{\npublic:\n node* head, *tail;\n int size = 0;\n\npublic:\n linked_list()\n {\n head = NULL;\n tail = NULL;\n }\n\n void addLast(int n)\n {\n node* tmp = new node;\n tmp->data = n;\n tmp->next = NULL;\n\n if (head == NULL)\n {\n head = tmp;\n tail = tmp;\n }\n else\n {\n tail->next = tmp;\n tail = tail->next;\n }\n size++;\n\n\n }\n void display() {\n for (node* tmp = head; tmp != NULL; tmp = tmp->next) {\n cout << tmp->data << \" \";\n }\n cout << endl;\n\n }\n void removeFirst() {\n //write your code here\n }\n\n\n\n void testList() {\n for (node* temp = head; temp != NULL; temp = temp->next) {\n cout << temp->data << endl;\n }\n cout << size << endl;\n\n if (size > 0) {\n cout << tail->data << endl;\n }\n }\n};\n\n\nint main() {\n\n\n string str;\n linked_list l;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n else if (str[0] == 'a') {\n string ss = str.substr(8, 2);\n int n = stoi(ss);\n l.addLast( n);\n\n }\n else if (str[0] == 's') {\n cout << l.size << endl;\n }\n else if (str[0] == 'd') {\n l.display();\n }\n else if (str[0] == 'r') {\n l.removeFirst();\n }\n }\n\n\n return 0;\n\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node next;\r\n }\r\n\r\n public static class LinkedList {\r\n Node head;\r\n Node tail;\r\n int size;\r\n\r\n void addLast(int val) {\r\n Node temp = new Node();\r\n temp.data = val;\r\n temp.next = null;\r\n\r\n if (size == 0) {\r\n head = tail = temp;\r\n } else {\r\n tail.next = temp;\r\n tail = temp;\r\n }\r\n\r\n size++;\r\n }\r\n\r\n public int size(){\r\n return size;\r\n }\r\n\r\n public void display(){\r\n for(Node temp = head; temp != null; temp = temp.next){\r\n System.out.print(temp.data + \" \");\r\n }\r\n System.out.println();\r\n }\r\n\r\n public void removeFirst(){\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n LinkedList list = new LinkedList();\r\n\r\n String str = br.readLine();\r\n while(str.equals(\"quit\") == false){\r\n if(str.startsWith(\"addLast\")){\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n list.addLast(val);\r\n } else if(str.startsWith(\"size\")){\r\n System.out.println(list.size());\r\n } else if(str.startsWith(\"display\")){\r\n list.display();\r\n } else if(str.startsWith(\"removeFirst\")){\r\n list.removeFirst();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n def __init__(self, data=None):\n self.data = data\n self.next = None\nclass SLinkedList:\n def __init__(self):\n self.head = None\n self.size=0\n self.tail=None\n\n def AddLast(self, newdata):\n NewNode = Node(newdata)\n if self.head is None:\n self.head = NewNode\n self.tail = NewNode\n self.size+=1\n return\n laste = self.head\n while(laste.next):\n laste = laste.next\n laste.next=NewNode\n self.tail=laste.next \n self.size+=1\n\n def display(self):\n printval = self.head\n while printval is not None:\n print (printval.data,end=\" \")\n printval = printval.next\n print(\" \")\n def testList(self) :\n temp = self.head\n while temp is not None: \n print(temp.data)\n temp = temp.next\n \n print(self.size)\n\n if self.size > 0 :\n \n print(self.tail.data)\n \n \n def removeFirst(self) :\n # write your code here\n\n\n\nl1 = SLinkedList()\nwhile 1>0 :\n str=input()\n\n if str[0]=='q':\n \n break;\n\n elif str[0]=='a':\n \n val=int(str[-3]+str[-2])\n \n l1.AddLast(val)\n \n elif str[0]=='d':\n \n\n l1.display() \n \n elif str[0]=='s':\n \n\n print(l1.size) \n \n elif str[0]=='r':\n \n\n l1.removeFirst()"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\naddLast 20\r\naddLast 30\r\ndisplay\r\nremoveFirst\r\nsize\r\naddLast 40\r\naddLast 50\r\nremoveFirst\r\ndisplay\r\nsize\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nremoveFirst\r\nquit","sampleOutput":"10 20 30 \r\n2\r\n30 40 50 \r\n3\r\nList is empty","questionVideo":"https://www.youtube.com/embed/3za9tOwFyCY","hints":[],"associated":[{"id":"238a1b16-5884-4818-889b-a9838a2dfa1d","name":"The two parts of node in singly linked list are :-(Q- Remove First In LinkedList)","slug":"the-two-parts-of-node-in-singly-linked-list-are-q-remove-first-in-linkedlist","type":4},{"id":"60647db9-fda9-4d3a-a8bc-d39c2583d7d9","name":"If the size of the linked list is one, which of the following is correct way to implement to remove the first node?(Q- Remove First In LinkedList)","slug":"if-the-size-of-the-linked-list-is-one-which-of-the-following-is-correct-way-to-implement-to-remove-the-first-node-q-remove-first-in-linkedlist","type":4},{"id":"6f79e3d7-f921-4dad-add8-9c6f5a6f8e90","name":"Which of the following is/are being updated after removeing the first node to the linked list if the list is not empty?(q - Remove First In LinkedList)","slug":"which-of-the-following-is-are-being-updated-after-removeing-the-first-node-to-the-linked-list-if-the-list-is-not-empty-q-remove-first-in-linkedlist","type":4},{"id":"b911179d-ae61-44bd-b235-94f1d7f39337","name":"What would be the time complexity of this problem?(Q- Remove First In LinkedList)","slug":"what-would-be-the-time-complexity-of-this-problem-q-remove-first-in-linkedlist","type":4}],"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":"91027ef1-2784-45bf-8143-cc6af4560105","name":"Linked Lists For Beginners","slug":"linked-lists-for-beginners","type":0},{"id":"f9abd6f3-2797-4f00-bdf1-829cd76f72b1","name":"Remove First In Linkedlist","slug":"remove-first-in-linkedlist","type":1}],"next":{"id":"fea21342-951a-48d9-b496-50141dcd1e62","name":"Remove First In LinkedList","type":3,"slug":"remove-first-in-linkedlist"},"prev":{"id":"8a25a8e6-6fb7-42f1-9217-7d0f578dee2b","name":"Display a Linked List","type":3,"slug":"display-a-linked-list"}}}
plane

Editor


Loading...

Remove First In Linkedlist

easy

1. You are given a partially written LinkedList class. 2. Here is a list of existing functions: 2.1 addLast - adds a new element with given value to the end of Linked List 2.2. display - Prints the elements of linked list from front to end in a single line. All elements are separated by space 2.3. size - Returns the number of elements in the linked list. 3. You are required to complete the body of removeFirst function 3.1. removeFirst - This function is required to remove the first element from Linked List. Also, if there is only one element, this should set head and tail to null. If there are no elements, this should print "List is empty". 4. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

addLast 10 addLast 20 addLast 30 display removeFirst size addLast 40 addLast 50 removeFirst display size removeFirst removeFirst removeFirst removeFirst quit

Sample Output

10 20 30 2 30 40 50 3 List is empty

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode