当前位置:网站首页>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;
};
边栏推荐
- 7-18 simple simulation of banking business queue
- Revit secondary development - project file to family file
- Force deduction - question 561 - array splitting I - step by step parsing
- 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
- Revit secondary development - Hide occlusion elements
- Unity development --- the mouse controls the camera to move, rotate and zoom
- 6-3 find the table length of the linked table
- Robot autonomous exploration DSVP: code parsing
- ASP.NET Core入门五
- OpenGL configuration vs2019
猜你喜欢

Form组件常用校验规则-2(持续更新中~)

What does it mean to prefix a string with F?

数字化转型:五个步骤推动企业进步

IP network active evaluation system -- x-vision

How pyGame rotates pictures

How to judge whether the input content is "number"

Blender exchange group, welcome to the water group ~

Select sort (illustration +c code)

Cannot find module 'xxx' or its corresponding type declaration

Vs custom template - take the custom class template as an example
随机推荐
Aspose. Word operation word document (I)
CTF练习
The whole network "chases" Zhong Xuegao
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
Amesim2016 and matlab2017b joint simulation environment construction
JS number is insufficient, and 0 is added
ASP.NET Core入门五
Gazebo import the mapping model created by blender
「开源摘星计划」Loki实现Harbor日志的高效管理
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
How to close eslint related rules
OpenGL configuration vs2019
Matplotlib quick start
What is the difference between the three values of null Nan undefined in JS
Add get disabled for RC form
Micro service remote debug, nocalhost + rainbow micro service development second bullet
OpenGL jobs - shaders
OpenGL job - texture
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
Px4 autonomous flight