{"id":"629a8074-83b9-4fc8-a98d-1fc98246f624","name":"Add Last In Linked List","description":"1. You are given a partially written LinkedList class.\r\n2. You are required to complete the body of addLast function. This function is supposed to add an element to the end 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.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n\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 // Write your code here\n \n\n }\n void display() {\n for (node* tmp = head; tmp != NULL; tmp = tmp->next) {\n cout << tmp->data << \" \";\n }\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\n int main() {\n\n \n string str;\nlinked_list l;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n if (str[0] == 'a') {\n string ss = str.substr(8, 2);\n int n = stoi(ss);\n l.addLast( n);\n\n }\n\n }\n\n l.testList();\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 // Write your code here\r\n }\r\n }\r\n\r\n public static void testList(LinkedList list) {\r\n for (Node temp = list.head; temp != null; temp = temp.next) {\r\n System.out.println(temp.data);\r\n }\r\n System.out.println(list.size);\r\n\r\n if (list.size > 0) {\r\n System.out.println(list.tail.data);\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 } \r\n str = br.readLine();\r\n }\r\n\r\n testList(list);\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 #write your code here\n\n def listprint(self):\n printval = self.head\n while printval is not None:\n print (printval.data)\n printval = printval.next\n\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\nl1 = SLinkedList()\nwhile 1>0 :\n str=input()\n\n if str[0]=='q':\n \n break;\n\n if str[0]=='a':\n \n val=int(str[-3]+str[-2])\n \n l1.AddLast(val)\n \n \nl1.testList()"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\naddLast 20\r\naddLast 30\r\naddLast 40\r\naddLast 50\r\nquit","sampleOutput":"10\r\n20\r\n30\r\n40\r\n50\r\n5\r\n50","questionVideo":"https://www.youtube.com/embed/r9FxRiIZhK4","hints":[],"associated":[{"id":"06362c3e-a274-4e4e-af34-3fd829aa178e","name":"What would be the time complexity of this problem if tail is not given and only head of the linked list is given?(Q- Add Last In Linked List)","slug":"what-would-be-the-time-complexity-of-this-problem-if-tail-is-not-given-and-only-head-of-the-linked-list-is-given-q-add-last-in-linked-list","type":4},{"id":"29bf79cc-8ac5-495e-b04d-ae48c3296347","name":"To add a node at the end of the linked list, the function is called with which parameter?(Q- Add Last In Linked List)","slug":"to-add-a-node-at-the-end-of-the-linked-list-the-function-is-called-with-which-parameter-q-add-last-in-linked-list","type":4},{"id":"30f363e4-5b8f-476b-9064-0c27245cab0b","name":"Any node in the linked list contains the data and _________?(Q- Add Last In Linked List)","slug":"any-node-in-the-linked-list-contains-the-data-and-q-add-last-in-linked-list","type":4},{"id":"4bc1910c-036c-4f72-8e7f-2ba6c3280b2d","name":"If the size of the linked list is zero, which of the following should be implemented to add the last node?(Q- Add Last In Linked List)","slug":"if-the-size-of-the-linked-list-is-zero-which-of-the-following-should-be-implemented-to-add-the-last-node-q-add-last-in-linked-list","type":4},{"id":"c494f7df-4a08-44d7-abf1-6b4b985739e5","name":"Which of the following is/are being updated after adding the last node to the linked list if the list is not empty?(Q- Add Last In Linked List)","slug":"which-of-the-following-is-are-being-updated-after-adding-the-last-node-to-the-linked-list-if-the-list-is-not-empty-q-add-last-in-linked-list","type":4},{"id":"f7cbca2a-a8bc-4b0c-bf8b-9e1c0167f2e7","name":"The node that we add at the last of the linked list will contain ______ in its pointer part?(Q- Add Last In Linked List)","slug":"the-node-that-we-add-at-the-last-of-the-linked-list-will-contain-in-its-pointer-part-q-add-last-in-linked-list","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":"4cf48f3c-e312-4419-ab90-004fccc1f672","name":"Add Last In Linked List","slug":"add-last-in-linked-list","type":1}],"next":{"id":"8230d09f-bbaf-46d6-af08-9c6be2c87510","name":"Add Last in Linked List","type":3,"slug":"add-last-in-linked-list"},"prev":{"id":"4fe4efc6-9e61-40f3-abe6-d5fb1bf1af29","name":"Linked List introduction","type":3,"slug":"linked-list-introduction"}}}

Add Last In Linked List

1. You are given a partially written LinkedList class. 2. You are required to complete the body of addLast function. This function is supposed to add an element to the end 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.

{"id":"629a8074-83b9-4fc8-a98d-1fc98246f624","name":"Add Last In Linked List","description":"1. You are given a partially written LinkedList class.\r\n2. You are required to complete the body of addLast function. This function is supposed to add an element to the end 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.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include <iostream>\n\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 // Write your code here\n \n\n }\n void display() {\n for (node* tmp = head; tmp != NULL; tmp = tmp->next) {\n cout << tmp->data << \" \";\n }\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\n int main() {\n\n \n string str;\nlinked_list l;\n while (true) {\n getline(cin, str);\n if (str[0] == 'q') {\n break;\n }\n if (str[0] == 'a') {\n string ss = str.substr(8, 2);\n int n = stoi(ss);\n l.addLast( n);\n\n }\n\n }\n\n l.testList();\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 // Write your code here\r\n }\r\n }\r\n\r\n public static void testList(LinkedList list) {\r\n for (Node temp = list.head; temp != null; temp = temp.next) {\r\n System.out.println(temp.data);\r\n }\r\n System.out.println(list.size);\r\n\r\n if (list.size > 0) {\r\n System.out.println(list.tail.data);\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 } \r\n str = br.readLine();\r\n }\r\n\r\n testList(list);\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 #write your code here\n\n def listprint(self):\n printval = self.head\n while printval is not None:\n print (printval.data)\n printval = printval.next\n\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\nl1 = SLinkedList()\nwhile 1>0 :\n str=input()\n\n if str[0]=='q':\n \n break;\n\n if str[0]=='a':\n \n val=int(str[-3]+str[-2])\n \n l1.AddLast(val)\n \n \nl1.testList()"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\naddLast 20\r\naddLast 30\r\naddLast 40\r\naddLast 50\r\nquit","sampleOutput":"10\r\n20\r\n30\r\n40\r\n50\r\n5\r\n50","questionVideo":"https://www.youtube.com/embed/r9FxRiIZhK4","hints":[],"associated":[{"id":"06362c3e-a274-4e4e-af34-3fd829aa178e","name":"What would be the time complexity of this problem if tail is not given and only head of the linked list is given?(Q- Add Last In Linked List)","slug":"what-would-be-the-time-complexity-of-this-problem-if-tail-is-not-given-and-only-head-of-the-linked-list-is-given-q-add-last-in-linked-list","type":4},{"id":"29bf79cc-8ac5-495e-b04d-ae48c3296347","name":"To add a node at the end of the linked list, the function is called with which parameter?(Q- Add Last In Linked List)","slug":"to-add-a-node-at-the-end-of-the-linked-list-the-function-is-called-with-which-parameter-q-add-last-in-linked-list","type":4},{"id":"30f363e4-5b8f-476b-9064-0c27245cab0b","name":"Any node in the linked list contains the data and _________?(Q- Add Last In Linked List)","slug":"any-node-in-the-linked-list-contains-the-data-and-q-add-last-in-linked-list","type":4},{"id":"4bc1910c-036c-4f72-8e7f-2ba6c3280b2d","name":"If the size of the linked list is zero, which of the following should be implemented to add the last node?(Q- Add Last In Linked List)","slug":"if-the-size-of-the-linked-list-is-zero-which-of-the-following-should-be-implemented-to-add-the-last-node-q-add-last-in-linked-list","type":4},{"id":"c494f7df-4a08-44d7-abf1-6b4b985739e5","name":"Which of the following is/are being updated after adding the last node to the linked list if the list is not empty?(Q- Add Last In Linked List)","slug":"which-of-the-following-is-are-being-updated-after-adding-the-last-node-to-the-linked-list-if-the-list-is-not-empty-q-add-last-in-linked-list","type":4},{"id":"f7cbca2a-a8bc-4b0c-bf8b-9e1c0167f2e7","name":"The node that we add at the last of the linked list will contain ______ in its pointer part?(Q- Add Last In Linked List)","slug":"the-node-that-we-add-at-the-last-of-the-linked-list-will-contain-in-its-pointer-part-q-add-last-in-linked-list","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":"4cf48f3c-e312-4419-ab90-004fccc1f672","name":"Add Last In Linked List","slug":"add-last-in-linked-list","type":1}],"next":{"id":"8230d09f-bbaf-46d6-af08-9c6be2c87510","name":"Add Last in Linked List","type":3,"slug":"add-last-in-linked-list"},"prev":{"id":"4fe4efc6-9e61-40f3-abe6-d5fb1bf1af29","name":"Linked List introduction","type":3,"slug":"linked-list-introduction"}}}
plane

Editor


Loading...

Add Last In Linked List

easy

1. You are given a partially written LinkedList class. 2. You are required to complete the body of addLast function. This function is supposed to add an element to the end 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.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

addLast 10 addLast 20 addLast 30 addLast 40 addLast 50 quit

Sample Output

10 20 30 40 50 5 50

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode