赞
踩
设计链表的实现。
您可以选择使用单链表或双链表。
单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-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
通过题目的描述,我们很容易得出这是一个 没有头节点的链表 ,当它被实例化时,只不过是有了一个头指针,用来代表整个链表
.
因此类内需要定义一个头指针,而链表最重要的就是头指针和结点,所以我们也用struct定义一个结点的结构。
class MyLinkedList { public: MyLinkedList() { } int get(int index) { } void addAtHead(int val) { } void addAtTail(int val) { } void addAtIndex(int index, int val) { } void deleteAtIndex(int index) { } private: struct Node{ int data; Node* nxt; }; Node* head; };
因为有了头节点,所以对链表的操作也主要由头节点发起。
这里需要遍历结点的函数的共同点就是:参数有 index
而遍历结点我采用的方法是:
用一个int类型的cnt计数,一个Node*类型的cur来表示遍历的进度
//这里为了提高程序阅读性,有需要可以更改 int cnt=0; Node* cur=head; while(true) { //如果找到了index的这个位置 if(cnt==index) break; //如果到了最后一个结点还没找到 if(cur->nxt==NULL) break; //cur继续遍历,cnt计数+1 cnt++; cur=cur->nxt; }
Node* r=new Node;
r->data=val;
r->nxt=NULL;
Node* cur=head;
if(head==NULL)
{
head=r;
return;
}
if(head==NULL)
{
head=new Node;
head->data=val;
head->nxt=NULL;
return;
}
class MyLinkedList { public: /** Initialize your data structure here. */ MyLinkedList() { 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<0) return -1; if(index==0) return head->data; int cnt=0; Node* cur=head; while(true) { if(cnt==index) return cur->data; if(cur->nxt==NULL) break; cnt++; cur=cur->nxt; } 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) { if(head==NULL) { head=new Node; head->data=val; head->nxt=NULL; return; } Node* r=new Node; r->data=val; r->nxt=head; head=r; } /** Append a node of value val to the last element of the linked list. */ void addAtTail(int val) { Node* r=new Node; r->data=val; r->nxt=NULL; Node* cur=head; if(head==NULL) { head=r; return; } //可以简化,但为了可读性 while(true) { if(cur->nxt==NULL) break; cur=cur->nxt; } cur->nxt=r; } /** 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<=0) { addAtHead(val); return; } int cnt=0; Node* cur=head; Node* r=new Node; while(true) { if(cnt==index-1) break; if(cur->nxt==NULL) break; cur=cur->nxt; cnt++; } //已经遍历到了最后一个结点 if(cur->nxt==NULL) { //如果index超过链表长度,不插入 if(index>cnt+1) return; //如果index的长度刚好和链表长度相等 if(index==cnt+1) { addAtTail(val); return; } } r->data=val; r->nxt=cur->nxt; cur->nxt=r; } /** Delete the index-th node in the linked list, if the index is valid. */ void deleteAtIndex(int index) { if(index<0) return; if(index==0) { head=head->nxt; return; } int cnt=0,flag=0; Node* cur=head; while(true) { if(cnt==index-1) break; if(cur->nxt==NULL) break; cur=cur->nxt; cnt++; } if(cur->nxt==NULL) return; cur->nxt=cur->nxt->nxt; } private: struct Node{ int data; Node* nxt; }; Node* head; }; /** * 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); */
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。