力扣 - 707. 设计链表
作者:互联网
目录
题目
思路1(单链表)
- 用单链表来实现
- 用一个head作为头结点,不存储数据
- 再用size存储当前链表长度,防止操作时候越界
代码
class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head;
/** Initialize your data structure here. */
public MyLinkedList() {
head = new ListNode(-1);
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;
}
ListNode cur = head;
while (index >= 0) {
cur = cur.next;
index--;
}
return cur.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 < 0 || index > size) {
return;
}
size++;
ListNode pre = head;
while (index > 0) {
pre = pre.next;
index--;
}
ListNode n = new ListNode(val);
n.next = pre.next;
pre.next = n;
}
/** 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--;
ListNode pre = head;
while (index > 0) {
pre = pre.next;
index--;
}
pre.next = pre.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);
*/
复杂度分析
- 时间复杂度:
- get:\(O(N)\)
- addAtHead:\(O(1)\)
- addAtTail:\(O(N)\)
- addAtIndex:\(O(N)\)
- deleteAtIndex:\(O(N)\)
- 空间复杂度:\(O(N)\),其中 N 为链表长度
思路2(双链表)
- 使用双链表来实现
- 再思路1基础上再加上一个尾指针tail,这样在尾部添加元素时直复杂度就是O(1),不用从头再遍历了
- 也用一个size存储链表长度
代码
class ListNode {
int val;
ListNode pre;
ListNode next;
public ListNode() {
}
public ListNode(int x) {
val = x;
}
}
class MyLinkedList {
int size;
ListNode head;
ListNode tail;
/** Initialize your data structure here. */
public MyLinkedList() {
head = new ListNode();
tail = new ListNode();
head.next = tail;
tail.pre = head;
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) {
ListNode p = head;
while (index >= 0) {
p = p.next;
index--;
}
return p.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) {
ListNode n = new ListNode(val);
n.next = head.next;
n.pre = head;
n.pre.next = n;
n.next.pre = n;
size++;
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
ListNode n = new ListNode(val);
n.pre = tail.pre;
n.next = tail;
n.next.pre = n;
n.pre.next = n;
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) {
if (index >= 0 && index < size + 1) {
ListNode p = head;
ListNode n = new ListNode(val);
while (index > 0) {
p = p.next;
index--;
}
n.next = p.next;
n.pre = p;
n.next.pre = n;
n.pre.next = n;
size++;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index >= 0 && index < size) {
ListNode p = head;
while (index > 0) {
p = p.next;
index--;
}
p.next = p.next.next;
p.next.pre = p;
size--;
}
}
}
/**
* 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);
*/
复杂度分析
- 时间复杂度:
- get:\(O(N)\)
- addAtHead:\(O(1)\)
- addAtTail:\(O(1)\)
- addAtIndex:\(O(N)\)
- deleteAtIndex:\(O(N)\)
- 空间复杂度:\(O(N)\),其中 N 为链表长度
标签:pre,力扣,ListNode,val,index,int,707,next,链表 来源: https://www.cnblogs.com/linzedian/p/13944367.html