跟之前Day 10 - Linked List - Design Linked List是一樣的題目,但為了區分單向和雙向的解法,因此文章標題稍有不同。
Design your implementation of the linked list. You can choose to use a singly or 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.
Implement the MyLinkedList
class:
MyLinkedList()
Initializes the MyLinkedList
object.int get(int index)
Get the value of the indexth
node in the linked list. If the index is invalid, return 1
.void addAtHead(int 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.void addAtTail(int val)
Append a node of value val
as the last element of the linked list.void addAtIndex(int index, int val)
Add a node of value val
before the indexth
node in the linked list. If index
equals the length of the linked list, the node will be appended to the end of the linked list. If index
is greater than the length, the node will not be inserted.void deleteAtIndex(int index)
Delete the indexth
node in the linked list, if the index is valid.Example 1:
Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]
Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
myLinkedList.get(1); // return 2
myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
myLinkedList.get(1); // return 3
Constraints:
0 <= index, val <= 1000
2000
calls will be made to get
, addAtHead
, addAtTail
, addAtIndex
and deleteAtIndex
.有了Day 10 - Linked List - Design Linked List的經驗,只需要在原本的方法上加上操作前一個節點的邏輯就可以了,還有另外新增size
變數儲存節點數量,這樣在判斷上比較方便。
Runtime: 9 ms (64.08%)
Memory Usage: 43.9 MB (94.45%)
class MyLinkedList {
private DoublyNode head;
private int size;
public MyLinkedList() {
}
public int get(int index) {
DoublyNode temp = this.head;
int curr = 0;
while (temp != null) {
if (curr == index) return temp.val;
temp = temp.next;
curr++;
}
return -1;
}
public void addAtHead(int val) {
DoublyNode temp = this.head;
DoublyNode node = new DoublyNode(val, null, temp);
if (temp != null) temp.prev = node;
this.head = node;
this.size++;
}
public void addAtTail(int val) {
if (size == 0) this.addAtHead(val);
else {
DoublyNode temp = this.head;
while (temp.next != null) {
temp = temp.next;
}
DoublyNode node = new DoublyNode(val, temp, null);
temp.next = node;
this.size++;
}
}
public void addAtIndex(int index, int val) {
if (index == 0) this.addAtHead(val);
else if (index == size) this.addAtTail(val);
else if (index > size + 1) return ;
else {
DoublyNode temp = this.head;
int curr = 0;
while (temp != null) {
if (curr == index) {
DoublyNode prev = temp.prev;
DoublyNode node = new DoublyNode(val, prev, temp);
if (prev != null) prev.next = node;
temp.prev = node;
this.size++;
break;
}
temp = temp.next;
curr++;
}
}
}
public void deleteAtIndex(int index) {
if (index == 0) this.head = head.next;
else if (index > size + 1) return ;
else {
DoublyNode temp = this.head;
int curr = 0;
while (temp != null) {
if (curr == index) {
DoublyNode prev = temp.prev;
DoublyNode next = temp.next;
if (prev != null) prev.next = next;
if (next != null) next.prev = prev;
this.size--;
break;
}
temp = temp.next;
curr++;
}
}
}
}
class DoublyNode {
int val;
DoublyNode prev;
DoublyNode next;
public DoublyNode(int val, DoublyNode prev, DoublyNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/