Problem 889
Design Linked List
Medium · 20 pts · Linked List, Design
Problem
Implement a singly linked list with 0-based indices, starting empty. 'get i' prints the value at index i, or -1 if i is invalid. 'addAtHead v' and 'addAtTail v' insert v at the front or the back. 'addAtIndex i v' inserts v before index i; if i equals the length it appends, and if i is larger nothing happens. 'deleteAtIndex i' removes the element at index i if it exists. A dummy head node keeps the code for index 0 simple.
Input
The first line contains Q. Each of the next Q lines contains one operation. There is at least one get.
Output
Print the result of every get, one per line.
Constraints
1 <= Q <= 2000; 0 <= i, v <= 1000
Example input
6 addAtHead 1 addAtTail 3 addAtIndex 1 2 get 1 deleteAtIndex 1 get 1
Example output
2 3
Explanation
After addAtHead 1, addAtTail 3 and addAtIndex 1 2 the list is 1 2 3, so get 1 prints 2. After deleteAtIndex 1 it is 1 3, and get 1 prints 3.