LeetCode707. 设计链表
LeetCode707. 设计链表
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
- get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
- addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
- addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
- addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
- deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
示例:
MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3
提示:
所有val值都在 [1, 1000] 之内。
操作次数将在 [1, 1000] 之内。
请不要使用内置的 LinkedList 库。
单链表 - 无哨兵 - 有头有尾
class MyLinkedList {
Node head;
Node tail;
int size;
/** Initialize your data structure here. */
public MyLinkedList() {
}
/** 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 (!isElementIndex(index)) {
return -1;
}
return getNode(index).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. */
public void addAtHead(int val) {
// 空链表需要将head、tail都指向e
if (head == null) {
Node e = new Node(val, null);
head = e;
tail = e;
} else {
head = new Node(val, head);
}
size++;
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
// 空链表需要将head、tail都指向e
if (tail == null) {
Node e = new Node(val, null);
tail = e;
head = e;
} else {
Node e = new Node(val , null);
tail.next = e;
tail = e;
}
size++;
}
/** 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) {
// 如果 index 等于链表的长度,则该节点将附加到链表的末尾
if (index == size) {
addAtTail(val);
return;
}
// 如果 index 大于链表长度,则不会插入节点
if (index > size) {
return;
}
// 如果index小于0,则在头部插入节点
if (index < 0) {
addAtHead(val);
return;
}
// 不是空链表、合法索引
if (index == 0) {
addAtHead(val);
} else {
final Node pre = getNode(index - 1);
pre.next = new Node(val, pre.next);
size++;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (isElementIndex(index)) {
if (index == 0) {
head = head.next;
if (size == 1) {
tail = null;
}
} else {
Node pre = getNode(index - 1);
pre.next = pre.next.next;
if (index == size - 1) {
tail = pre;
}
}
size--;
}
}
private Node getNode(int index) {
Node e = head;
for (int i = 0; i < index; i++)
e = e.next;
return e;
}
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
}
}
单链表 - 无哨兵 - 有头无尾
class MyLinkedList {
private Node head;
private int size;
/** Initialize your data structure here. */
public MyLinkedList() {
}
/** 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 (isElementIndex(index)) {
return getNode(index).val;
}
return -1;
}
/** 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. */
public void addAtHead(int val) {
addAtIndex(0, val);
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
addAtIndex(size, 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. */
public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
if (index <= 0) {
head = new Node(val, head);
size++;
return;
}
// if (index == size) {
// Node last = getNode(size - 1);
// last.next = new Node(val, null);
// size++;
// return;
// }
Node pre = getNode(index - 1);
Node e = new Node(val, pre.next);
pre.next = e;
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (isElementIndex(index)) {
if (index == 0) {
head = head.next;
} else {
Node pre = getNode(index - 1);
pre.next = pre.next.next;
}
size--;
}
}
public Node getNode(int index) {
Node e = head;
for (int i = 0; i < index; i++)
e = e.next;
return e;
}
public boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
}
}
单链表 - 哨兵
class MyLinkedList {
Node dummyHead;
int size;
/** Initialize your data structure here. */
public MyLinkedList() {
dummyHead = new Node(-1, null);
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 (!isElementIndex(index)) {
return -1;
}
return getNode(index).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. */
public void addAtHead(int val) {
addAtIndex(0, val);
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
addAtIndex(size, 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. */
public void addAtIndex(int index, int val) {
if (index > size) {
return;
}
if (index == size) {
Node last = getNode(size - 1);
last.next = new Node(val, null);
size++;
return;
}
if (index <= 0) {
Node e = new Node(val, dummyHead.next);
dummyHead.next = e;
size++;
return;
}
Node pre = getNode(index - 1);
Node e = new Node(val, pre.next);
pre.next = e;
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (isElementIndex(index)) {
Node pre = getNode(index - 1);
pre.next = pre.next.next;
size--;
}
}
public Node getNode(int index) {
Node e = dummyHead;
for (int i = -1; i < index; i++)
e = e.next;
return e;
}
public boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
}
}
双向链表 - 无哨兵 - 有头有尾
class MyLinkedList {
Node head;
Node tail;
int size;
/** Initialize your data structure here. */
public MyLinkedList() {
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 (isElementIndex(index)) {
return getNode(index).val;
}
return -1;
}
/** 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. */
public void addAtHead(int val) {
addAtIndex(0, val);
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
addAtIndex(size, 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. */
public void addAtIndex(int index, int val) {
if (index > size)
return;
if (index == size) {
if (size == 0) {
head = new Node(val, null, null);
tail = head;
} else {
Node e = new Node(val, tail, null);
tail.next = e;
tail = e;
}
size++;
return;
}
if (index <= 0) {
Node e = new Node(val, null, head);
head.pre = e;
head = e;
size++;
return;
}
Node pre = getNode(index - 1);
Node e = new Node(val, pre, pre.next);
pre.next.pre = e;
pre.next = e;
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (isElementIndex(index)) {
if (size == 1) {
head = null;
tail = null;
} else {
if (index == 0) {
head = head.next;
head.pre = null;
} else if (index == size - 1) {
tail = tail.pre;
tail.next = null;
} else {
Node pre = getNode(index - 1);
pre.next.next.pre = pre;
pre.next = pre.next.next;
}
}
size--;
}
}
public Node getNode(int index) {
Node e = head;
for (int i = 0; i < index; i++)
e = e.next;
return e;
}
public boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
class Node {
int val;
Node pre;
Node next;
public Node(int val, Node pre, Node next) {
this.val = val;
this.pre = pre;
this.next = next;
}
}
}