707 设计单链表
第一次完成這樣的設(shè)計(jì),一路磕磕碰碰,遇到了許多問(wèn)題,最后終于一一解決了。感恩https://blog.csdn.net/lym940928/article/details/81276658
題目如下:
設(shè)計(jì)鏈表的實(shí)現(xiàn)。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節(jié)點(diǎn)應(yīng)該具有兩個(gè)屬性:val?和?next。val?是當(dāng)前節(jié)點(diǎn)的值,next?是指向下一個(gè)節(jié)點(diǎn)的指針/引用。如果要使用雙向鏈表,則還需要一個(gè)屬性?prev?以指示鏈表中的上一個(gè)節(jié)點(diǎn)。假設(shè)鏈表中的所有節(jié)點(diǎn)都是 0-index 的。
在鏈表類(lèi)中實(shí)現(xiàn)這些功能:
- get(index):獲取鏈表中第?index?個(gè)節(jié)點(diǎn)的值。如果索引無(wú)效,則返回-1。
- addAtHead(val):在鏈表的第一個(gè)元素之前添加一個(gè)值為?val?的節(jié)點(diǎn)。插入后,新節(jié)點(diǎn)將成為鏈表的第一個(gè)節(jié)點(diǎn)。
- addAtTail(val):將值為?val?的節(jié)點(diǎn)追加到鏈表的最后一個(gè)元素。
- addAtIndex(index,val):在鏈表中的第?index?個(gè)節(jié)點(diǎn)之前添加值為?val? 的節(jié)點(diǎn)。如果?index?等于鏈表的長(zhǎng)度,則該節(jié)點(diǎn)將附加到鏈表的末尾。如果?index?大于鏈表長(zhǎng)度,則不會(huì)插入節(jié)點(diǎn)。
- deleteAtIndex(index):如果索引?index?有效,則刪除鏈表中的第?index?個(gè)節(jié)點(diǎn)。
下面是給的示例:
MyLinkedList linkedList = new MyLinkedList(); //創(chuàng)建鏈表 linkedList.addAtHead(1); //在頭節(jié)點(diǎn)加入元素1,鏈表變?yōu)?->nullptr linkedList.addAtTail(3); //在尾節(jié)點(diǎn)加入元素3,鏈表變?yōu)?->3->nullptr linkedList.addAtIndex(1,2); //鏈表變?yōu)?-> 2-> 3->nullptr linkedList.get(1); //返回2 linkedList.deleteAtIndex(1); //現(xiàn)在鏈表是1-> 3->nullptr linkedList.get(1); //返回3 class MyLinkedList {private: //聲明一個(gè)鏈表的結(jié)構(gòu),單鏈表中有一個(gè)next的指針,一個(gè)val的值,還有一個(gè)聲明節(jié)點(diǎn)的函數(shù)struct LinkNode{int val;LinkNode* next;LinkNode(int x):val(x), next(nullptr){}};int len; //代表鏈表的長(zhǎng)度LinkNode *head;//頭指針 LinkNode *tail;//尾指針public:/**Initialize your data structure here*/MyLinkedList(){head=new LinkNode(0); //初始化鏈表,頭指針指向的元素為0,next為nullptrtail=head;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){LinkNode *pCur;pCur=head;if(index>len) return -1;elsefor(int i=0;i<=index;i++)pCur=pCur->next; return pCur->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){LinkNode *pCur=new LinkNode(val);pCur->next=head;head=pCur;len++; return;}/** Append a node of value val to the last element of the linked list. */void addAtTail(int val){LinkNode *pCur=new LinkNode(val);tail=head;if(tail->next!=nullptr)tail=tail->next; tail->next=pCur;pCur->next=nullptr;len++;return; }/** 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)return;LinkNode *pCur;LinkNode *add=new LinkNode(val);pCur=head;for(int i=0;i<index;i++)pCur=pCur->next;add->next=pCur->next;pCur->next=add;len++;return;}/** Delete the index-th node in the linked list, if the index is valid. */void deleteAtIndex(int index){if(index>=len) return;LinkNode *pCur;pCur=head;for(int i=0;i<index;i++)pCur=pCur->next;LinkNode *del = pCur->next;pCur->next = del->next;del->next = NULL;len--;return;}};?
總結(jié)
- 上一篇: 排骨的功效与作用、禁忌和食用方法
- 下一篇: leetcode 二进制求和 addBi