{"id":"b0c97b83-c49b-490f-b12e-f2873680602c","name":"Add Before In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of addBefore function. This function is supposed to add value before given Node. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\".\r\n4. If Index is Invalid then return \"IndexIsInValid: -1\". \r\n5. You are required to update head, tail and size as required.\r\n6. Input and Output is managed for you. Just update the code in incomplete 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":""},"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 // Exceptions========================================\r\n\r\n private boolean ListIsEmptyException() {\r\n if (this.size == 0) {\r\n System.out.print(\"ListIsEmpty: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n private boolean indexIsInvalidException(int index, int leftRange, int rightRange) {\r\n if (index < leftRange || index > rightRange) {\r\n System.out.print(\"IndexIsInValid: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // BasicFunctions======================================\r\n\r\n public int size() {\r\n return this.size;\r\n }\r\n\r\n public boolean isEmpty() {\r\n return this.size == 0;\r\n }\r\n\r\n // AddFunctions======================================\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 private void addLastNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n this.tail.next = node;\r\n node.prev = this.tail;\r\n this.tail = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addLast(int val) {\r\n Node node = new Node(val);\r\n addLastNode(node);\r\n }\r\n\r\n private void addNodeAt(int index, Node node) {\r\n if (index == 0)\r\n addFirstNode(node);\r\n else if (index == this.size)\r\n addLastNode(node);\r\n else {\r\n Node forw = getNodeAt(index);\r\n Node prev = forw.prev;\r\n\r\n prev.next = node;\r\n node.prev = prev;\r\n\r\n node.next = forw;\r\n forw.prev = node;\r\n\r\n this.size++;\r\n }\r\n }\r\n\r\n public void addAt(int index, int data) {\r\n if (indexIsInvalidException(index, 0, this.size))\r\n System.out.println(-1);\r\n else {\r\n Node node = new Node(data);\r\n addNodeAt(index, node);\r\n }\r\n }\r\n\r\n public void addBefore(Node refNode, int data) {\r\n // complete this function.\r\n }\r\n\r\n public void addBefore(int idx, int data) {\r\n Node node = getNodeAt(idx);\r\n addBefore(node, data);\r\n }\r\n\r\n // RemoveFunctions======================================\r\n\r\n private Node removeFirstNode() {\r\n Node node = this.head;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node nextNode = this.head.next;\r\n nextNode.prev = null;\r\n node.next = null;\r\n\r\n this.head = nextNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeFirstNode();\r\n return node.data;\r\n }\r\n\r\n private Node removeLastNode() {\r\n Node node = this.tail;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node prevNode = this.tail.prev;\r\n prevNode.next = null;\r\n node.prev = null;\r\n\r\n this.tail = prevNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeLastNode();\r\n return node.data;\r\n }\r\n\r\n private Node removeAtNode(int index) {\r\n if (index == 0)\r\n return removeFirstNode();\r\n else if (index == this.size - 1)\r\n return removeLastNode();\r\n else {\r\n Node node = getNodeAt(index);\r\n Node prev = node.prev;\r\n Node forw = node.next;\r\n\r\n prev.next = forw;\r\n forw.prev = prev;\r\n\r\n node.next = null;\r\n node.prev = null;\r\n\r\n this.size--;\r\n return node;\r\n }\r\n }\r\n\r\n public int removeAt(int index) {\r\n if (ListIsEmptyException())\r\n return -1;\r\n if (indexIsInvalidException(index, 0, this.size - 1))\r\n return -1;\r\n\r\n Node node = removeAtNode(index);\r\n return node.data;\r\n }\r\n\r\n // getFunctions======================================\r\n\r\n public int getFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.head.data;\r\n }\r\n\r\n public int getLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.tail.data;\r\n }\r\n\r\n private Node getNodeAt(int index) {\r\n Node curr = this.head;\r\n while (index-- > 0)\r\n curr = curr.next;\r\n\r\n return curr;\r\n }\r\n\r\n public int getAt(int index) {\r\n if (ListIsEmptyException())\r\n return -1;\r\n else if (indexIsInvalidException(index, 0, this.size - 1))\r\n return -1;\r\n\r\n Node node = getNodeAt(index);\r\n return node.data;\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 else if (s[0].equals(\"removeFirst\"))\r\n System.out.println(dll.removeFirst());\r\n else if (s[0].equals(\"removeLast\"))\r\n System.out.println(dll.removeLast());\r\n else if (s[0].equals(\"getFirst\"))\r\n System.out.println(dll.getFirst());\r\n else if (s[0].equals(\"getLast\"))\r\n System.out.println(dll.getLast());\r\n else if (s[0].equals(\"size\"))\r\n System.out.println(dll.size());\r\n else if (s[0].equals(\"isEmpty\"))\r\n System.out.println(dll.isEmpty());\r\n else if (s[0].equals(\"getAt\"))\r\n System.out.println(dll.getAt(Integer.parseInt(s[1])));\r\n else if (s[0].equals(\"addAt\"))\r\n dll.addAt(Integer.parseInt(s[1]), Integer.parseInt(s[2]));\r\n else if (s[0].equals(\"removeAt\"))\r\n dll.removeAt(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"addBefore\"))\r\n dll.addBefore(Integer.parseInt(s[1]), Integer.parseInt(s[2]));\r\n\r\n str = scn.nextLine();\r\n }\r\n\r\n System.out.println(dll);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"addFirst 4\r\naddFirst 4\r\naddLast 5\r\naddFirst 7\r\ngetAt 4 3\r\naddAt 4 34\r\naddAt 0 43\r\nremoveAt 4\r\naddBefore 2 33\r\naddBefore 0 33\r\naddBefore 4 33\r\nremoveAt 0\r\naddAt 8 545\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nsize\r\nisEmpty\r\ngetFirst\r\nremoveFirst\r\nremoveLast\r\ngetLast\r\nremoveFirst\r\nremoveFirst\r\naddAt 0 345\r\nstop","sampleOutput":"IndexIsInValid: -1\r\nIndexIsInValid: -1\r\n43\r\n7\r\n6\r\nfalse\r\n33\r\n33\r\n1\r\n34\r\n33\r\n4\r\n[345, 4, 34]\r\n","questionVideo":"","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":"5cbdcdf4-1957-4741-973d-f19a2fe1ccda","name":"Add Before In Doubly Linkedlist","slug":"add-before-in-doubly-linkedlist","type":1}],"next":{"id":"3bacdb6b-e157-4e96-9a0e-09e7b52fbe8c","name":"Add After In Doubly Linkedlist","type":1,"slug":"add-after-in-doubly-linkedlist"},"prev":{"id":"a969441a-db34-42ac-b337-a66b921a40f1","name":"Remove At In Doubly Linkedlist","type":3,"slug":"remove-at-in-doubly-linkedlist"}}}

Add Before In Doubly Linkedlist

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of addBefore function. This function is supposed to add value before given Node. 3. If size of list is zero then return "ListIsEmpty: -1". 4. If Index is Invalid then return "IndexIsInValid: -1". 5. You are required to update head, tail and size as required. 6. Input and Output is managed for you. Just update the code in incomplete 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":"b0c97b83-c49b-490f-b12e-f2873680602c","name":"Add Before In Doubly Linkedlist","description":"1. You are given a partially written DoublyLinkedList class.\r\n2. You are required to complete the body of addBefore function. This function is supposed to add value before given Node. \r\n3. If size of list is zero then return \"ListIsEmpty: -1\".\r\n4. If Index is Invalid then return \"IndexIsInValid: -1\". \r\n5. You are required to update head, tail and size as required.\r\n6. Input and Output is managed for you. Just update the code in incomplete 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":""},"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 // Exceptions========================================\r\n\r\n private boolean ListIsEmptyException() {\r\n if (this.size == 0) {\r\n System.out.print(\"ListIsEmpty: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n private boolean indexIsInvalidException(int index, int leftRange, int rightRange) {\r\n if (index < leftRange || index > rightRange) {\r\n System.out.print(\"IndexIsInValid: \");\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n // BasicFunctions======================================\r\n\r\n public int size() {\r\n return this.size;\r\n }\r\n\r\n public boolean isEmpty() {\r\n return this.size == 0;\r\n }\r\n\r\n // AddFunctions======================================\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 private void addLastNode(Node node) {\r\n if (this.size == 0)\r\n this.head = this.tail = node;\r\n else {\r\n this.tail.next = node;\r\n node.prev = this.tail;\r\n this.tail = node;\r\n }\r\n this.size++;\r\n }\r\n\r\n public void addLast(int val) {\r\n Node node = new Node(val);\r\n addLastNode(node);\r\n }\r\n\r\n private void addNodeAt(int index, Node node) {\r\n if (index == 0)\r\n addFirstNode(node);\r\n else if (index == this.size)\r\n addLastNode(node);\r\n else {\r\n Node forw = getNodeAt(index);\r\n Node prev = forw.prev;\r\n\r\n prev.next = node;\r\n node.prev = prev;\r\n\r\n node.next = forw;\r\n forw.prev = node;\r\n\r\n this.size++;\r\n }\r\n }\r\n\r\n public void addAt(int index, int data) {\r\n if (indexIsInvalidException(index, 0, this.size))\r\n System.out.println(-1);\r\n else {\r\n Node node = new Node(data);\r\n addNodeAt(index, node);\r\n }\r\n }\r\n\r\n public void addBefore(Node refNode, int data) {\r\n // complete this function.\r\n }\r\n\r\n public void addBefore(int idx, int data) {\r\n Node node = getNodeAt(idx);\r\n addBefore(node, data);\r\n }\r\n\r\n // RemoveFunctions======================================\r\n\r\n private Node removeFirstNode() {\r\n Node node = this.head;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node nextNode = this.head.next;\r\n nextNode.prev = null;\r\n node.next = null;\r\n\r\n this.head = nextNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeFirstNode();\r\n return node.data;\r\n }\r\n\r\n private Node removeLastNode() {\r\n Node node = this.tail;\r\n if (this.size == 1)\r\n this.head = this.tail = null;\r\n else {\r\n Node prevNode = this.tail.prev;\r\n prevNode.next = null;\r\n node.prev = null;\r\n\r\n this.tail = prevNode;\r\n }\r\n\r\n this.size--;\r\n return node;\r\n }\r\n\r\n public int removeLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n Node node = removeLastNode();\r\n return node.data;\r\n }\r\n\r\n private Node removeAtNode(int index) {\r\n if (index == 0)\r\n return removeFirstNode();\r\n else if (index == this.size - 1)\r\n return removeLastNode();\r\n else {\r\n Node node = getNodeAt(index);\r\n Node prev = node.prev;\r\n Node forw = node.next;\r\n\r\n prev.next = forw;\r\n forw.prev = prev;\r\n\r\n node.next = null;\r\n node.prev = null;\r\n\r\n this.size--;\r\n return node;\r\n }\r\n }\r\n\r\n public int removeAt(int index) {\r\n if (ListIsEmptyException())\r\n return -1;\r\n if (indexIsInvalidException(index, 0, this.size - 1))\r\n return -1;\r\n\r\n Node node = removeAtNode(index);\r\n return node.data;\r\n }\r\n\r\n // getFunctions======================================\r\n\r\n public int getFirst() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.head.data;\r\n }\r\n\r\n public int getLast() {\r\n if (ListIsEmptyException())\r\n return -1;\r\n\r\n return this.tail.data;\r\n }\r\n\r\n private Node getNodeAt(int index) {\r\n Node curr = this.head;\r\n while (index-- > 0)\r\n curr = curr.next;\r\n\r\n return curr;\r\n }\r\n\r\n public int getAt(int index) {\r\n if (ListIsEmptyException())\r\n return -1;\r\n else if (indexIsInvalidException(index, 0, this.size - 1))\r\n return -1;\r\n\r\n Node node = getNodeAt(index);\r\n return node.data;\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 else if (s[0].equals(\"removeFirst\"))\r\n System.out.println(dll.removeFirst());\r\n else if (s[0].equals(\"removeLast\"))\r\n System.out.println(dll.removeLast());\r\n else if (s[0].equals(\"getFirst\"))\r\n System.out.println(dll.getFirst());\r\n else if (s[0].equals(\"getLast\"))\r\n System.out.println(dll.getLast());\r\n else if (s[0].equals(\"size\"))\r\n System.out.println(dll.size());\r\n else if (s[0].equals(\"isEmpty\"))\r\n System.out.println(dll.isEmpty());\r\n else if (s[0].equals(\"getAt\"))\r\n System.out.println(dll.getAt(Integer.parseInt(s[1])));\r\n else if (s[0].equals(\"addAt\"))\r\n dll.addAt(Integer.parseInt(s[1]), Integer.parseInt(s[2]));\r\n else if (s[0].equals(\"removeAt\"))\r\n dll.removeAt(Integer.parseInt(s[1]));\r\n else if (s[0].equals(\"addBefore\"))\r\n dll.addBefore(Integer.parseInt(s[1]), Integer.parseInt(s[2]));\r\n\r\n str = scn.nextLine();\r\n }\r\n\r\n System.out.println(dll);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"addFirst 4\r\naddFirst 4\r\naddLast 5\r\naddFirst 7\r\ngetAt 4 3\r\naddAt 4 34\r\naddAt 0 43\r\nremoveAt 4\r\naddBefore 2 33\r\naddBefore 0 33\r\naddBefore 4 33\r\nremoveAt 0\r\naddAt 8 545\r\naddLast 1\r\nremoveFirst\r\nremoveFirst\r\nsize\r\nisEmpty\r\ngetFirst\r\nremoveFirst\r\nremoveLast\r\ngetLast\r\nremoveFirst\r\nremoveFirst\r\naddAt 0 345\r\nstop","sampleOutput":"IndexIsInValid: -1\r\nIndexIsInValid: -1\r\n43\r\n7\r\n6\r\nfalse\r\n33\r\n33\r\n1\r\n34\r\n33\r\n4\r\n[345, 4, 34]\r\n","questionVideo":"","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":"5cbdcdf4-1957-4741-973d-f19a2fe1ccda","name":"Add Before In Doubly Linkedlist","slug":"add-before-in-doubly-linkedlist","type":1}],"next":{"id":"3bacdb6b-e157-4e96-9a0e-09e7b52fbe8c","name":"Add After In Doubly Linkedlist","type":1,"slug":"add-after-in-doubly-linkedlist"},"prev":{"id":"a969441a-db34-42ac-b337-a66b921a40f1","name":"Remove At In Doubly Linkedlist","type":3,"slug":"remove-at-in-doubly-linkedlist"}}}
plane

Editor


Loading...

Add Before In Doubly Linkedlist

medium

1. You are given a partially written DoublyLinkedList class. 2. You are required to complete the body of addBefore function. This function is supposed to add value before given Node. 3. If size of list is zero then return "ListIsEmpty: -1". 4. If Index is Invalid then return "IndexIsInValid: -1". 5. You are required to update head, tail and size as required. 6. Input and Output is managed for you. Just update the code in incomplete 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 getAt 4 3 addAt 4 34 addAt 0 43 removeAt 4 addBefore 2 33 addBefore 0 33 addBefore 4 33 removeAt 0 addAt 8 545 addLast 1 removeFirst removeFirst size isEmpty getFirst removeFirst removeLast getLast removeFirst removeFirst addAt 0 345 stop

Sample Output

IndexIsInValid: -1 IndexIsInValid: -1 43 7 6 false 33 33 1 34 33 4 [345, 4, 34]

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode