Comment on page

## 题目描述

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
addAtHead(val) : Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
addAtTail(val) : Append a node of value val to the last element of the linked list.
addAtIndex(index, val) : Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.
Example:
Input:
[[],[1],[3],[1,2],[1],[1],[1]]
Output:
[null,null,null,null,2,null,3]
Explanation:
Constraints:
0 <= index,val <= 1000

## 代码

### Approach #1 Singly Linked List

Complexity Analysis
• Time complexity: O(1) for addAtHead. O(k) for get, addAtIndex, and deleteAtIndex, where k is an index of the element to get, add or delete. O(N) for addAtTail.
• Space complexity: O(1) for all operations.
class ListNode {
int val;
ListNode next;
ListNode(int x) {val = x;};
}
int size;
/** Initialize your data structure here. */
size = 0;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if (index < 0 || index >= size) return -1;
for (int i = 0; i < index + 1; i++) {
curr = curr.next;
}
return curr.val;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
}
/** Append a node of value val to the last element of the linked list. */
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if (index > size) return;
if (index < 0) return 0;
size++;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) return;
size--;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
pred.next = pred.next.next;
}
}
/**
* int param_1 = obj.get(index);
* obj.deleteAtIndex(index);
*/

### Approach #2 Doubly Linked List

Complexity Analysis
• Space complexity: O(1) for all operations.
class ListNode {
int val;
ListNode next;
ListNode prev;
ListNode(int x) { val = x; }
}
int size;
size = 0;
tail = new ListNode(0);
}
public int get(int index) {
if (index < 0 || index >= size) return -1;
if (index + 1 < size - index) {
for (int i = 0; i< index + 1; i++) {
curr = curr.next;
}
} else {
curr = tail;
for (int i = 0; i < size - index; i++) {
curr = curr.prev;
}
}
return curr.val;
}
size++;
}
ListNode succ = tail, pred = tail.prev;
size++;
}
public void addAtIndex(int index, int val) {
if (index > size) return;
if (index < 0) index = 0;
ListNode pred, succ;
if (index < size - index) {
for (int i = 0; i < index; i++) {
pred = pred.next;
}
succ = pred.next;
} else {
succ = tail;
for (int i = 0; i < size - index; i++) {
succ = succ.prev;
}
}
size++;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) return;
ListNode pred, succ;
if (index < size - index) {
for (int i = 0; i < index; i++) {
pred = pred.next;
}
succ = pred.next.next;
} else {
succ = tail;
for (int i = 0; i < size - index - 1; i++) {
succ = succ.prev;
}
pred = succ.prev.prev;
}
size--;
pred.next = succ;
succ.prev = pred;
}
}