{"id":"8c7eabc7-8de5-4446-a226-24cf954fc881","name":"Get Value In Linked List","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. \r\n All elements are separated by space.\r\n2.3. size - Returns the number of elements in the linked list.\r\n2.4. removeFirst - Removes the first element from Linked List. \r\n3. You are required to complete the body of getFirst, getLast and getAt function \r\n3.1. getFirst - Should return the data of first element. If empty should return -1 and print \"List is empty\".\r\n3.2. getLast - Should return the data of last element. If empty should return -1 and print \"List is empty\".\r\n3.3. getAt - Should return the data of element available at the index passed. If empty should return -1 and print \"List is empty\". If invalid index is passed, should return -1 and print \"Invalid arguments\".\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>\nusing namespace std;\nclass node{\n public:\n int val;\n node* next;\n \n\n};\nclass LinkedList {\n public: node* head=nullptr;\n node* tail=nullptr;\n int size_=0;\n\nvoid insert_at_tail(int val){\n if(head==NULL){\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n head=newnode;\n\n }\n else{\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n node *temp = head;\n while(temp->next!=NULL){\n temp=temp->next;\n }\n temp->next = newnode;\n }\n\n}\n\nvoid print (){\n node *temp =head;\n if(head==NULL){\n cout << \"0\" << endl;\n return;\n }\n while(temp!=NULL){\n cout<<temp->val<<\" \";\n temp=temp->next;\n }\n cout << endl;\n}\nvoid deletion_at_head(){\n if(head==NULL) return;\n node *temp=head;\n head=head->next;\n delete temp;\n\n}\n\nint size(){\n int cnt=0;\n node* temp=head;\n while(temp!=NULL){\n temp=temp->next;\n cnt++;\n }\n return cnt;\n}\n\nvoid getFirst(){\n //write your code here\n}\n\nvoid getLast(){\n //write your code here\n}\n\nnode* getAt(int p){\n //write your code here\n}\n};\nint main() {\n LinkedList l1;\n string s;\n cin >> s;\n while(s!=\"quit\"){\n if(s==\"addLast\"){\n int data;\n cin>> data;\n l1.insert_at_tail(data);\n }\n else if(s==\"getFirst\"){\n if(l1.head!=NULL){\n l1.getFirst();\n }else{\n cout << \"List is empty\" << endl;\n }\n }\n else if(s==\"getLast\"){\n if(l1.head!=NULL){\n l1.getLast();\n } \n else\n {\n cout<<\"List is empty\";\n }\n \n }\n else if(s==\"removeFirst\"){\n if(l1.head!=NULL){\n l1.deletion_at_head();\n }\n else{\n cout << \"List is empty\" << endl; \n }\n }\n else if(s==\"getAt\"){\n if(l1.head!=NULL){\n int i;\n cin >> i; \n if(i>=l1.size()){\n cout << \"Invalid arguments\" << endl;\n }\n else{\n l1.getAt(i);\n }\n }\n else\n {\n cout<<\"List is empty\";\n }\n }\n else if(s==\"display\"){\n if(l1.head!=NULL){\n l1.print();\n }\n else{\n cout << endl;\n }\n }\n else if(s==\"size\"){\n if(l1.head!=NULL){\n cout << l1.size() << endl;\n }\n }\n cin>>s;\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 if(size == 0){\r\n System.out.println(\"List is empty\");\r\n } else if(size == 1){\r\n head = tail = null;\r\n size = 0;\r\n } else {\r\n head = head.next;\r\n size--;\r\n }\r\n }\r\n\r\n public int getFirst(){\r\n // write your code here\r\n }\r\n\r\n public int getLast(){\r\n // write your code here\r\n }\r\n\r\n public int getAt(int idx){\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 } else if(str.startsWith(\"getFirst\")){\r\n int val = list.getFirst();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"getLast\")){\r\n int val = list.getLast();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"getAt\")){\r\n int idx = Integer.parseInt(str.split(\" \")[1]);\r\n int val = list.getAt(idx);\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n\n def __init__(self):\n self.data = 0\n self.next = None\n\n\nclass LinkedList:\n\n def __init__(self):\n self.head = None\n self.tail = None\n self.size = 0\n\n def addLast(self, val):\n temp = Node()\n temp.data = val\n temp.next = None\n\n if self.size == 0:\n self.head = self.tail = temp\n else:\n self.tail.next = temp\n self.tail = temp\n self.size += 1\n \n def size(self):\n return self.size\n \n \n def display(self):\n temp = self.head\n while temp is not None:\n print(temp.data, end = \" \")\n temp = temp.next\n print()\n \n def removeFirst(self):\n if self.size == 0:\n print(\"List is empty\")\n elif self.size == 1:\n self.head = self.tail = None\n self.size = 0\n else:\n self.head = self.head.next\n self.size -= 1\n\n def getFirst(self):\n #write your code here\n\n def getLast(self):\n #write your code here\n \n\n def getAt(self, idx):\n #write your code here\n\nl1 = LinkedList()\nwhile True:\n cmd=input().split(\" \")\n if \"addLast\" in cmd[0]:\n val=int(cmd[1])\n l1.addLast(val)\n \n elif \"getFirst\" in cmd[0]:\n ans=l1.getFirst()\n if(ans!=-1):\n print(ans)\n \n elif \"getLast\" in cmd[0]:\n ans=l1.getLast()\n if(ans!=-1):\n print(ans)\n \n elif \"getAt\" in cmd[0]:\n idx=int(cmd[1])\n ans=l1.getAt(idx)\n if(ans!=-1):\n print(ans)\n \n elif \"removeFirst\" in cmd[0]:\n l1.removeFirst()\n \n elif \"quit\" in cmd[0]:\n break"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\ngetFirst\r\naddLast 20\r\naddLast 30\r\ngetFirst\r\ngetLast\r\ngetAt 1\r\naddLast 40\r\ngetLast\r\naddLast 50\r\nremoveFirst\r\ngetFirst\r\nremoveFirst\r\nremoveFirst\r\ngetAt 3\r\nremoveFirst\r\nremoveFirst\r\ngetFirst\r\nquit","sampleOutput":"10\r\n10\r\n30\r\n20\r\n40\r\n20\r\nInvalid arguments\r\nList is empty","questionVideo":"https://www.youtube.com/embed/t99GFv9vKZ0","hints":[],"associated":[{"id":"6cd141ec-49c6-413b-b33b-2af8c6fa9138","name":"What is the time ans space complexity to get value from last from the given linked list ?(Q- Get value in LinkedList)","slug":"what-is-the-time-ans-space-complexity-to-get-value-from-last-from-the-given-linked-list-q-get-value-in-linkedlist","type":4},{"id":"6e2da544-ba6f-4a9e-8c44-e9486ffdb547","name":"What is the space complexity for deleting a linked list?(Q- Get value in LinkedList)","slug":"what-is-the-space-complexity-for-deleting-a-linked-list-q-get-value-in-linkedlist","type":4},{"id":"ba2d2a78-2872-4756-827d-fb06368aaddc","name":"What is the time ans space complexity to get value from first from the given linked list ?(Q- Get value in LinkedList)","slug":"what-is-the-time-ans-space-complexity-to-get-value-from-first-from-the-given-linked-list-q-get-value-in-linkedlist","type":4},{"id":"e370d3d6-b8fd-451f-95ca-ce9e34dcaaf5","name":"What is the time ans space complexity to get value at the index passed from the given linked list ?(Q- Get value in LinkedList)","slug":"what-is-the-time-ans-space-complexity-to-get-value-at-the-index-passed-from-the-given-linked-list-q-get-value-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":"9c38c0fb-d174-472c-af6f-d02dbfde14ec","name":"Get Value In Linked List","slug":"get-value-in-linked-list","type":1}],"next":{"id":"ed7ecf0b-a58d-42f9-87e5-c417a70dde52","name":"Get Value in Linked List","type":3,"slug":"get-value-in-linked-list"},"prev":{"id":"fea21342-951a-48d9-b496-50141dcd1e62","name":"Remove First In LinkedList","type":3,"slug":"remove-first-in-linkedlist"}}}

Get Value In Linked List

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. 2.4. removeFirst - Removes the first element from Linked List. 3. You are required to complete the body of getFirst, getLast and getAt function 3.1. getFirst - Should return the data of first element. If empty should return -1 and print "List is empty". 3.2. getLast - Should return the data of last element. If empty should return -1 and print "List is empty". 3.3. getAt - Should return the data of element available at the index passed. If empty should return -1 and print "List is empty". If invalid index is passed, should return -1 and print "Invalid arguments". 4. Input and Output is managed for you.

{"id":"8c7eabc7-8de5-4446-a226-24cf954fc881","name":"Get Value In Linked List","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. \r\n All elements are separated by space.\r\n2.3. size - Returns the number of elements in the linked list.\r\n2.4. removeFirst - Removes the first element from Linked List. \r\n3. You are required to complete the body of getFirst, getLast and getAt function \r\n3.1. getFirst - Should return the data of first element. If empty should return -1 and print \"List is empty\".\r\n3.2. getLast - Should return the data of last element. If empty should return -1 and print \"List is empty\".\r\n3.3. getAt - Should return the data of element available at the index passed. If empty should return -1 and print \"List is empty\". If invalid index is passed, should return -1 and print \"Invalid arguments\".\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>\nusing namespace std;\nclass node{\n public:\n int val;\n node* next;\n \n\n};\nclass LinkedList {\n public: node* head=nullptr;\n node* tail=nullptr;\n int size_=0;\n\nvoid insert_at_tail(int val){\n if(head==NULL){\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n head=newnode;\n\n }\n else{\n node *newnode = new node;\n newnode->val=val;\n newnode->next=NULL;\n node *temp = head;\n while(temp->next!=NULL){\n temp=temp->next;\n }\n temp->next = newnode;\n }\n\n}\n\nvoid print (){\n node *temp =head;\n if(head==NULL){\n cout << \"0\" << endl;\n return;\n }\n while(temp!=NULL){\n cout<<temp->val<<\" \";\n temp=temp->next;\n }\n cout << endl;\n}\nvoid deletion_at_head(){\n if(head==NULL) return;\n node *temp=head;\n head=head->next;\n delete temp;\n\n}\n\nint size(){\n int cnt=0;\n node* temp=head;\n while(temp!=NULL){\n temp=temp->next;\n cnt++;\n }\n return cnt;\n}\n\nvoid getFirst(){\n //write your code here\n}\n\nvoid getLast(){\n //write your code here\n}\n\nnode* getAt(int p){\n //write your code here\n}\n};\nint main() {\n LinkedList l1;\n string s;\n cin >> s;\n while(s!=\"quit\"){\n if(s==\"addLast\"){\n int data;\n cin>> data;\n l1.insert_at_tail(data);\n }\n else if(s==\"getFirst\"){\n if(l1.head!=NULL){\n l1.getFirst();\n }else{\n cout << \"List is empty\" << endl;\n }\n }\n else if(s==\"getLast\"){\n if(l1.head!=NULL){\n l1.getLast();\n } \n else\n {\n cout<<\"List is empty\";\n }\n \n }\n else if(s==\"removeFirst\"){\n if(l1.head!=NULL){\n l1.deletion_at_head();\n }\n else{\n cout << \"List is empty\" << endl; \n }\n }\n else if(s==\"getAt\"){\n if(l1.head!=NULL){\n int i;\n cin >> i; \n if(i>=l1.size()){\n cout << \"Invalid arguments\" << endl;\n }\n else{\n l1.getAt(i);\n }\n }\n else\n {\n cout<<\"List is empty\";\n }\n }\n else if(s==\"display\"){\n if(l1.head!=NULL){\n l1.print();\n }\n else{\n cout << endl;\n }\n }\n else if(s==\"size\"){\n if(l1.head!=NULL){\n cout << l1.size() << endl;\n }\n }\n cin>>s;\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 if(size == 0){\r\n System.out.println(\"List is empty\");\r\n } else if(size == 1){\r\n head = tail = null;\r\n size = 0;\r\n } else {\r\n head = head.next;\r\n size--;\r\n }\r\n }\r\n\r\n public int getFirst(){\r\n // write your code here\r\n }\r\n\r\n public int getLast(){\r\n // write your code here\r\n }\r\n\r\n public int getAt(int idx){\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 } else if(str.startsWith(\"getFirst\")){\r\n int val = list.getFirst();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"getLast\")){\r\n int val = list.getLast();\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n } else if(str.startsWith(\"getAt\")){\r\n int idx = Integer.parseInt(str.split(\" \")[1]);\r\n int val = list.getAt(idx);\r\n if(val != -1){\r\n System.out.println(val);\r\n }\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class Node:\n\n def __init__(self):\n self.data = 0\n self.next = None\n\n\nclass LinkedList:\n\n def __init__(self):\n self.head = None\n self.tail = None\n self.size = 0\n\n def addLast(self, val):\n temp = Node()\n temp.data = val\n temp.next = None\n\n if self.size == 0:\n self.head = self.tail = temp\n else:\n self.tail.next = temp\n self.tail = temp\n self.size += 1\n \n def size(self):\n return self.size\n \n \n def display(self):\n temp = self.head\n while temp is not None:\n print(temp.data, end = \" \")\n temp = temp.next\n print()\n \n def removeFirst(self):\n if self.size == 0:\n print(\"List is empty\")\n elif self.size == 1:\n self.head = self.tail = None\n self.size = 0\n else:\n self.head = self.head.next\n self.size -= 1\n\n def getFirst(self):\n #write your code here\n\n def getLast(self):\n #write your code here\n \n\n def getAt(self, idx):\n #write your code here\n\nl1 = LinkedList()\nwhile True:\n cmd=input().split(\" \")\n if \"addLast\" in cmd[0]:\n val=int(cmd[1])\n l1.addLast(val)\n \n elif \"getFirst\" in cmd[0]:\n ans=l1.getFirst()\n if(ans!=-1):\n print(ans)\n \n elif \"getLast\" in cmd[0]:\n ans=l1.getLast()\n if(ans!=-1):\n print(ans)\n \n elif \"getAt\" in cmd[0]:\n idx=int(cmd[1])\n ans=l1.getAt(idx)\n if(ans!=-1):\n print(ans)\n \n elif \"removeFirst\" in cmd[0]:\n l1.removeFirst()\n \n elif \"quit\" in cmd[0]:\n break"}},"points":10,"difficulty":"easy","sampleInput":"addLast 10\r\ngetFirst\r\naddLast 20\r\naddLast 30\r\ngetFirst\r\ngetLast\r\ngetAt 1\r\naddLast 40\r\ngetLast\r\naddLast 50\r\nremoveFirst\r\ngetFirst\r\nremoveFirst\r\nremoveFirst\r\ngetAt 3\r\nremoveFirst\r\nremoveFirst\r\ngetFirst\r\nquit","sampleOutput":"10\r\n10\r\n30\r\n20\r\n40\r\n20\r\nInvalid arguments\r\nList is empty","questionVideo":"https://www.youtube.com/embed/t99GFv9vKZ0","hints":[],"associated":[{"id":"6cd141ec-49c6-413b-b33b-2af8c6fa9138","name":"What is the time ans space complexity to get value from last from the given linked list ?(Q- Get value in LinkedList)","slug":"what-is-the-time-ans-space-complexity-to-get-value-from-last-from-the-given-linked-list-q-get-value-in-linkedlist","type":4},{"id":"6e2da544-ba6f-4a9e-8c44-e9486ffdb547","name":"What is the space complexity for deleting a linked list?(Q- Get value in LinkedList)","slug":"what-is-the-space-complexity-for-deleting-a-linked-list-q-get-value-in-linkedlist","type":4},{"id":"ba2d2a78-2872-4756-827d-fb06368aaddc","name":"What is the time ans space complexity to get value from first from the given linked list ?(Q- Get value in LinkedList)","slug":"what-is-the-time-ans-space-complexity-to-get-value-from-first-from-the-given-linked-list-q-get-value-in-linkedlist","type":4},{"id":"e370d3d6-b8fd-451f-95ca-ce9e34dcaaf5","name":"What is the time ans space complexity to get value at the index passed from the given linked list ?(Q- Get value in LinkedList)","slug":"what-is-the-time-ans-space-complexity-to-get-value-at-the-index-passed-from-the-given-linked-list-q-get-value-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":"9c38c0fb-d174-472c-af6f-d02dbfde14ec","name":"Get Value In Linked List","slug":"get-value-in-linked-list","type":1}],"next":{"id":"ed7ecf0b-a58d-42f9-87e5-c417a70dde52","name":"Get Value in Linked List","type":3,"slug":"get-value-in-linked-list"},"prev":{"id":"fea21342-951a-48d9-b496-50141dcd1e62","name":"Remove First In LinkedList","type":3,"slug":"remove-first-in-linkedlist"}}}
plane

Editor


Loading...

Get Value In Linked List

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. 2.4. removeFirst - Removes the first element from Linked List. 3. You are required to complete the body of getFirst, getLast and getAt function 3.1. getFirst - Should return the data of first element. If empty should return -1 and print "List is empty". 3.2. getLast - Should return the data of last element. If empty should return -1 and print "List is empty". 3.3. getAt - Should return the data of element available at the index passed. If empty should return -1 and print "List is empty". If invalid index is passed, should return -1 and print "Invalid arguments". 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 getFirst addLast 20 addLast 30 getFirst getLast getAt 1 addLast 40 getLast addLast 50 removeFirst getFirst removeFirst removeFirst getAt 3 removeFirst removeFirst getFirst quit

Sample Output

10 10 30 20 40 20 Invalid arguments List is empty

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode