财务公司网站建设,wordpress博客站搭建,wordpress设置图片切换时间,网页设计素材有两种分别是什么文章目录1. 设计一个单链表2. 双向链表1. 设计一个单链表
在链表类中实现这些功能#xff1a;
get(index)#xff1a;获取链表中第 index 个节点的值。如果索引无效#xff0c;则返回-1。 addAtHead(val)#xff1a;在链表的第一个元素之前添加一个值为 val 的节点。插入…
文章目录1. 设计一个单链表2. 双向链表1. 设计一个单链表
在链表类中实现这些功能
get(index)获取链表中第 index 个节点的值。如果索引无效则返回-1。 addAtHead(val)在链表的第一个元素之前添加一个值为 val 的节点。插入后新节点将成为链表的第一个节点。 addAtTail(val)将值为 val 的节点追加到链表的最后一个元素。 addAtIndex(index,val)在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度则该节点将附加到链表的末尾。如果 index 大于链表长度则不会插入节点。如果index小于0则在头部插入节点。 deleteAtIndex(index)如果索引 index 有效则删除链表中的第 index 个节点。
来源力扣LeetCode 链接https://leetcode-cn.com/problems/design-linked-list 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
class node
{
public:int val;node *next;node(int v):val(v),next(NULL) {}
};
class MyLinkedList {node *head, *tail;int len;
public:/** Initialize your data structure here. */MyLinkedList() {head tail NULL;len 0;}/** 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 len || index 0)return -1;node *cur head;while(index--)cur cur-next;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. */void addAtHead(int val) { node *h new node(val);h-next head;head h;if(len 0)tail head;len;}/** Append a node of value val to the last element of the linked list. */void addAtTail(int val) {node *t new node(val);if(len 0){head tail t;}else{tail-next t;tail t;}len; }/** 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 len)addAtTail(val);else if(index 0)addAtHead(val);else if(index len)return;else{node *cur head;while(--index)cur cur-next;node *newNode new node(val);newNode-next cur-next;cur-next newNode;len;}}/** Delete the index-th node in the linked list, if the index is valid. */void deleteAtIndex(int index) {if(index len || index 0)return;--len;node *virtualheadNode, *del, *cur;virtualheadNode new node(0);virtualheadNode-next head;cur virtualheadNode;while(index--){cur cur-next;}del cur-next;cur-next cur-next-next;delete del;head virtualheadNode-next;if(cur-next NULL)tail cur;delete virtualheadNode;}
};2. 双向链表
class node
{
public:int val;node *next;node *prev;node(int v):val(v),next(NULL),prev(NULL) {}
};
class MyLinkedList {node *head, *tail;int len;
public:/** Initialize your data structure here. */MyLinkedList() {head tail NULL;len 0;}/** 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 len || index 0)return -1;node *cur head;while(index--)cur cur-next;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. */void addAtHead(int val) { node *h new node(val);h-next head;head h;if(len 0)tail head;len;}/** Append a node of value val to the last element of the linked list. */void addAtTail(int val) {node *t new node(val);if(len 0){head tail t;}else{tail-next t;t-prev tail;tail t;}len; }/** 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 len)addAtTail(val);else if(index 0)addAtHead(val);else if(index len)return;else{node *cur head;while(--index)cur cur-next;node *newNode new node(val);newNode-next cur-next;newNode-prev cur;cur-next-prev newNode;cur-next newNode;len;}}/** Delete the index-th node in the linked list, if the index is valid. */void deleteAtIndex(int index) {if(index len || index 0)return;--len;node *virtualheadNode, *del, *cur;virtualheadNode new node(0);virtualheadNode-next head;head-prev virtualheadNode;cur virtualheadNode;while(index--){cur cur-next;}del cur-next;cur-next cur-next-next;if(del-next) del-next-prev cur;delete del;head virtualheadNode-next;if(cur-next NULL)tail cur;delete virtualheadNode;}
};