当前位置:网站首页>LeetCode707. Design linked list
LeetCode707. Design linked list
2022-07-07 22:49:00 【Qingshan's green shirt】
LeetCode707. Design the list
List of articles
1. subject
2. Ideas
There are five functions , Corresponding to the five basic operations of the linked list : Find nodes 、 Head insertion 、 Tail insertion 、 Insert before a node 、 Delete . The following are implemented separately .
3. Code implementation
(1) Implement by function
The function interface
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) {
}
};
/** * 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); */
1. Linked list design
private:
int size; // Chain length
LinkNode* dummyHead; // Sentinel node
2. Linked list creation and function initialization
public:
// Definition of linked list
struct LinkNode{
int val;
LinkNode* next;
ListNode() : val(0), next(nullptr) {
}
ListNode(int x) : val(x), next(nullptr) {
}
ListNode(int x, ListNode *next) : val(x), next(next) {
}
};
// Initialization of linked list
MyLinkedList() {
dummyHead = new LinkNode(0);// Sentinel node next The default point to NULL
size = 0; // The length of the list is 0 Sentinel nodes are not included in the length
}
matters needing attention
1. Used size, Indicates the length of the list , Sentinel nodes are not included !
2. Pay attention to the question stem ! The subscript of the node is from 0 Start !!!
3. Lookup function ——int get(int index) {}
// Find the first index Nodes
int get(int index) {
if(index > (size-1) || index < 0)//index from 0 Start !!! therefore size Need to get 1
return -1;
LinkNode *p = dummyHead -> next;// confirm dummy There's something in the back k You can rest assured ->next
while(index--)// First use and then subtract , Just go index Step
p = p->next;
return p->val;
}
matters needing attention
1. Be careful while(index–) Use , It's just right to go index Step . Can replace for.
2. another :index Get is index-1 The value after
3. Write dummyHead->next Pay attention dummyHead Is there free in the back !
4.index from 0 Start ! Not from 1!
4. Head insertion node —— void addAtHead(int val) {}
void addAtHead(int val) {
LinkNode* node = new LinkNode(val);
node->next = dummyHead->next; // Insert it behind the sentinel node
dummyHead->next = node;
size++;
}
matters needing attention
Don't forget to change the length of the linked list !
5. Tail insertion node ——void addAtTail(int val)
void addAtTail(int val) {
LinkNode *p = dummyHead;// Don't point casually dummy Of next!
while(p->next != NULL)
p = p->next;
LinkNode*node = new LinkNode(val);
//node->next = p->next;// Don't write , Because in the end !
p->next = node;
size++;
}
matters needing attention
1. When traversing the linked list, set the pointer for traversal , Be careful if you access empty areas .
6. Insert before a specific node ——void addAtIndex(int index, int val) {}
void addAtIndex(int index, int val) {
//index < 0 The situation of
if(index<0){
LinkNode*node = new LinkNode(val);
node->next = dummyHead->next;
dummyHead->next = node;
size++;}
//index>size( Chain length )
else if(index > size) return;
//index == Chain length
else if(index == size)
{
LinkNode *p = dummyHead;
while(p->next != NULL)
p = p->next;
LinkNode*node = new LinkNode(val);
node->next = p->next;
p->next = node;
size++;
}
// Other situations
else//if(index>=0 && index < size)// The conditions here are wrong , At present, I still don't know how to change ..
{
LinkNode *p = dummyHead;
//for(int i = 0;i<index;i++)// It's the same thing as down here !
while(index--)
p = p->next;//p Point to previous node
LinkNode*node = new LinkNode(val);
node->next = p->next;
p->next = node;
size++;
}
}
Stick a section here Carl The big guy's question , Because many of the above situations can be combined !
void addAtIndex(int index, int val) {
if (index > _size) {
return;
}
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
// You can use this !
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
matters needing attention
1. In many cases, never all if, Remember to if else!!! Otherwise, it will be troublesome if there is repetition !
2. If the condition of one situation is troublesome , It's better to put it at the end and use it directly else
Be careful size Relationship with the following table ! The length of the linked list is not size-1!
7. Delete —— void deleteAtIndex(int index)
void deleteAtIndex(int index) {
if(index >= size || index <0) return;// There has to be ? No need .
if(index <= (size-1) && index >=0)
{
LinkNode *p = dummyHead;
//for(int i = 0;i<index;i++)
while(index--) // Either way
p = p->next;//p Point to previous node
LinkNode * q = p->next;
p-> next = q->next;
delete q;
size--;
}
}
matters needing attention
1.
if(index >= size || index <0) return;
This sentence can be deleted .
2. The traversal pointer is still from dummyHead set out
(2) Complete code
class MyLinkedList {
public:
struct LinkNode{
int val;
LinkNode* next;
LinkNode(int x):val(x),next(nullptr){
}
};
MyLinkedList() {
dummyHead = new LinkNode(0);// Sentinel node
size = 0; // Chain length Sentinels are not length
}
int get(int index) {
// Find the first index Nodes
if(index > (size-1) || index < 0)//index from 0 Start !!! therefore size Need to get 1
return -1;
LinkNode *p = dummyHead -> next;// confirm dummy There is something in the back ->next
while(index--)// First use and then subtract , Just go index Step
p = p->next;
return p->val;
}
void addAtHead(int val) {
LinkNode* node = new LinkNode(val);
node->next = dummyHead->next;
dummyHead->next = node;
size++;
}
void addAtTail(int val) {
LinkNode *p = dummyHead;// Don't point casually dummy Of next
while(p->next != NULL)
p = p->next;
LinkNode*node = new LinkNode(val);
//node->next = p->next;
p->next = node;
size++;
}
void addAtIndex(int index, int val) {
if(index<0){
LinkNode*node = new LinkNode(val);
node->next = dummyHead->next;
dummyHead->next = node;
size++;
}
else if(index > size) return;// correct
else if(index == size)// It's not an empty list By the way, it should
{
LinkNode *p = dummyHead;
while(p->next != NULL)
p = p->next;
LinkNode*node = new LinkNode(val);
node->next = p->next;
p->next = node;
size++;
}
else //if(index>=0 && index < size)// It is required to be in the index Before nodes !
{
LinkNode *p = dummyHead;
//for(int i = 0;i<index;i++)
while(index--)
p = p->next;//p Point to previous node
LinkNode*node = new LinkNode(val);
node->next = p->next;
p->next = node;
size++;
}
}
void deleteAtIndex(int index) {
if(index >= size || index <0) return;// There has to be ?
if(index <= (size-1) && index >=0)
{
LinkNode *p = dummyHead;
//for(int i = 0;i<index;i++)
while(index--) // Either way
p = p->next;//p Point to previous node
LinkNode * q = p->next;
p-> next = q->next;
delete q;
size--;
}
}
private:
int size;
LinkNode* dummyHead;
};
边栏推荐
- Record a garbled code during servlet learning
- How to quickly check whether the opening area ratio of steel mesh conforms to ipc7525
- How to choose the appropriate automated testing tools?
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
- 变量与常量
- Robot autonomous exploration series papers environment code
- JS number is insufficient, and 0 is added
- XMIND mind mapping software sharing
- Variables and constants
- This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
猜你喜欢
Px4 autonomous flight
数字化转型:五个步骤推动企业进步
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Pdf document signature Guide
VTOL in Px4_ att_ Control source code analysis [supplement]
Redis官方ORM框架比RedisTemplate更优雅
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
How to realize the movement control of characters in horizontal game
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
随机推荐
Record problems fgui tween animation will be inexplicably killed
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
Pyqt GUI interface and logic separation
Overseas agent recommendation
Cannot find module 'xxx' or its corresponding type declaration
CTF练习
Ni9185 and ni9234 hardware settings in Ni Max
OpeGL personal notes - lights
Pdf document signature Guide
Robot autonomous exploration series papers environment code
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
What does it mean to prefix a string with F?
What is the difference between the three values of null Nan undefined in JS
C development -- WPF simple animation
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
php 获取图片信息的方法
筑起云端 “免疫”屏障,让你的数据有备无患
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
Use blocconsumer to build responsive components and monitor status at the same time
Debezium系列之:源码阅读之BinlogReader