Design Linked List
Problem statement
Design a linked list that starts empty and processes operations in order. Use linked nodes for the stored sequence.
For this exercise, rows are [kind,index,value] and indices are zero-based:
0: get the value at index, or-1when index is invalid; value is ignored.1: add value at the head; index is ignored.2: add value at the tail; index is ignored.3: insert value before index. An index at most zero means the head; index equal to length appends; index greater than length does nothing.4: delete the node at index if0 <= index < length; otherwise do nothing. Value is ignored.
Return one answer per row: get returns the observed value or -1; every other operation returns 0, including an ignored mutation. Duplicate and negative stored values are allowed. The interface and edge conventions are supplied for this practice.
Function
runLinkedList(operations: int[][]) → int[]Examples
Example 1
operations = [[1,0,1],[2,0,3],[3,1,2],[0,1,0],[4,1,0],[0,1,0]]return = [0,0,0,2,0,3]The list becomes [1], then [1,3], then [1,2,3]. Getting index 1 yields 2. Deleting it leaves [1,3], so the final get returns 3.
Example 2
operations = [[3,-2,8],[3,3,9],[0,-1,0],[4,-1,0],[0,0,0]]return = [0,0,-1,0,8]Negative add-at inserts 8 at the head. Add-at index 3 is too large and is ignored. Negative get returns -1 and negative delete does nothing; the final get returns 8.
Constraints
0 <= operations.length <= 1000operations[i].length == 3-1000000 <= operations[i][j] <= 1000000- Operation kinds are 0 through 4.
- Implement the sequence with linked nodes; do not use a built-in list as the data structure that stores its values.