← 返回 bytedance 的题目列表Implement a Doubly Linked List
类型:online_judge
Implement a doubly linked list with the following operations:
addAtHead(int val): Add a node with value val at the head of the list.
addAtTail(int val): Append a node with value val at the end of the list.
addAtIndex(int index, int val): Add a node with value val before the index-th node in the list. If index equals the length of the list, the node will be appended to the end. If index is greater than the length, the node will not be inserted. If index is less than 0, the node will be inserted at the head.
deleteAtIndex(int index): If the index is valid, delete the index-th node from the list.
Requirements:
All values are integers.
All operations have O(1) time complexity.
Return the list's state after executing all commands.
Example:
Input:
addAtHead(1)
addAtTail(3)
addAtIndex(1,2)
get(1)
deleteAtIndex(1)
get(1)
Output:
[1, 2, 3]
2
3
Data Constraints: Perform up to 1000 operations.
Example
Input
addAtHead 1
addAtTail 3
addAtIndex 1 2
get 1
deleteAtIndex 1
get 1