707. 设计链表
难度中等
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 库。
思路1.0:
尝试用智能指针编写这个链表类。
代码1.0(编译出错):
class MyLinkedList {
private:
int length;
shared_ptr<ListNode> head;
public:
struct ListNode {
int val;
shared_ptr<ListNode> next;
ListNode(int x) : val(x), next(NULL) {}
shared_ptr<ListNode>& operator=(const shared_ptr<ListNode>& rhs) {
return rhs->next;
}
};
/** Initialize your data structure here. */
MyLinkedList() :length(0), head(NULL) {}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if (index + 1 <= length) {
auto cur = head;
while (index != -1) {
cur = cur->next;
--index;
}
return cur->val;
}
else {
cout << "out of index,input correct data please..." << endl;
return INT_MAX;
}
}
/** 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 addAtHead(int val) {
auto behindList = head->next;
auto addNode = make_shared<ListNode>(val);
head->next = addNode;
addNode->next = behindList;
++length;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
auto cur = head;
while (cur != NULL) {
cur = cur->next;
}
auto addNode = make_shared<ListNode>(val);
cur->next = addNode;
++length;
}
/** 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. */
void addAtIndex(int index, int val) {
if (index + 1 <= length) {
auto cur = head;
while (index != 0) {
cur = cur->next;
--index;
}
auto addNode = make_shared<ListNode>(val);
auto behindList = cur->next;
cur->next = addNode;
addNode->next = behindList;
++length;
}
else {
cout << "out of index,input correct data please..." << endl;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if (index + 1 <= length) {
auto pre = head;
auto cur = head->next;
while (index != 0) {
pre = cur;
cur = cur->next;
--index;
}
auto behindList = cur->next;
pre->next = behindList;
--length;
}
else {
cout << "out of index,input correct data please..." << endl;
}
}
};
碰上了一个又一个问题,倒在了“cur=cur->next这一’='未重载”的问题上。先用普通指针把这题完成了吧,需要加强智能指针的使用。
代码2.0:
用普通指针完成了这个链表类,效率好低!对STL容器中这些方法的实现方法突然感兴趣了,那效能应该是拉满了的吧。
class MyLinkedList {
public:
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
/** Initialize your data structure here. */
MyLinkedList() :length(0), head(new ListNode(-1)) {}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if (index + 1 <= length) {
auto cur = head;
while (index != -1) {
cur = cur->next;
--index;
}
return cur->val;
}
else {
cout << "delete wrong,out of index,input correct data please..." << endl;
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. */
void addAtHead(int val) {
auto behindList = head->next;
auto addNode = new ListNode(val);
head->next = addNode;
addNode->next = behindList;
++length;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
auto cur = head;
while (cur ->next != NULL) {
cur = cur->next;
}
auto addNode = new ListNode(val);
cur->next = addNode;
++length;
}
/** 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. */
void addAtIndex(int index, int val) {
if (index < length) {
auto cur = head;
while (index != 0) {
cur = cur->next;
--index;
}
auto addNode = new ListNode(val);
auto behindList = cur->next;
cur->next = addNode;
addNode->next = behindList;
++length;
}
else if(index == length){
auto cur = head;
while (index != 0) {
cur = cur->next;
--index;
}
auto addNode = new ListNode(val);
cur->next = addNode;
++length;
}
else {
cout << "add wrong,out of index,input correct data please..." << endl;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if (index + 1 <= length) {
auto pre = head;
auto cur = head->next;
while (index != 0) {
pre = cur;
cur = cur->next;
--index;
}
auto behindList = cur->next;
pre->next = behindList;
delete cur;
--length;
}
else {
cout << "out of index,input correct data please..." << endl;
}
}
private:
int length;
ListNode* head;
};