Description
Design your implementation of the linked list. Implement get, addAtHead, addAtTail, addAtIndex, and deleteAtIndex operations.
Examples
Input:
["addAtHead",1],["addAtTail",3],["addAtIndex",1,2],["get",1],["deleteAtIndex",1],["get",1]Output:
[null,null,null,2,null,3]Explanation:
Linked list operations.
Input:
["addAtTail",5],["addAtTail",7],["get",0],["addAtHead",2],["get",0],["deleteAtIndex",0],["get",0]Output:
[null,null,5,null,2,null,5]Explanation:
Start with empty list. Add 5 at tail: [5]. Add 7 at tail: [5,7]. Get index 0 returns 5. Add 2 at head: [2,5,7]. Get index 0 returns 2. Delete index 0: [5,7]. Get index 0 returns 5.
Input:
["addAtIndex",0,10],["addAtIndex",1,20],["get",2],["deleteAtIndex",0],["deleteAtIndex",0],["get",0]Output:
[null,null,-1,null,null,-1]Explanation:
Add 10 at index 0: [10]. Add 20 at index 1: [10,20]. Get index 2 returns -1 (out of bounds). Delete index 0: [20]. Delete index 0: []. Get index 0 returns -1 (empty list).
Constraints
- •
0 ≤ index, val ≤ 1000