当前位置:网站首页>Leetcode -- linked list
Leetcode -- linked list
2022-06-24 09:20:00 【Rain curtain】
Linked list : Single chain list 、 Double linked list 、 Circular linked list .
The difference between linked list and array :
| add to / Delete | lookup | Applicable scenario | |
| Array | O(n) | O(1) | The data is fixed , Search frequently , Less additions and deletions |
| Linked list | O(1) | O(n) | Data is not fixed , Add and delete frequently , Search less |
203 Remove linked list elements
c++ Use the linked list to delete the elements delete Memory space of this element
Method 1 : If the header node is an element that needs to be deleted , Need to put head = head->next
ListNode* removeElements(ListNode* head, int val) {
//val Is the head node element
while (head != NULL && head->val == val)
{
ListNode* tmp = head;
head = head->next;
delete tmp;
}
ListNode* p = head;
//val Not a header node element
while(p != NULL && p->next != NULL){
if(p->next->val == val){
ListNode* tmp = p->next;
p->next = p->next->next;
delete tmp;
}else{
p = p->next;
}
}
return head;
}Method 2 : Set up a virtual head node ( It needs to be initialized ), Let the virtual head node next = head, Finally, remember to let head = Of the virtual head node next, Then delete the virtual head node
ListNode* removeElements(ListNode* head, int val) {
// Set up a virtual head node
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* p = dummyHead; //p Is the currently accessed node
while(p->next != NULL){
if(p->next->val == val){
ListNode* tmp = p->next;
p->next = p->next->next;
delete tmp;
}else{
p = p->next;
}
}
// Finally, delete the set virtual head node
head = dummyHead->next;
delete dummyHead;
return head;
}707 Design the list
For array traversal, you only need to know its length , The length of the linked list is unknown , You can only find one node at a time , Until... Of a node next = null
class MyLinkedList {
public:
// Define linked list node structure
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
// Initialize linked list
MyLinkedList() {
_dummyHead = new LinkedNode(0); // The head node defined here It's a virtual head node , Instead of the real chain header node
_size = 0;
}
// Get to page No index Node values , If index It's illegal. It's returned directly -1, Be careful index It's from 0 At the beginning , The first 0 The first node is the head node
int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;
while(index--){ // If --index You're going to fall into a dead circle
cur = cur->next;
}
return cur->val;
}
// Insert a node at the front of the linked list , After insertion , The newly inserted node is the new head node of the linked list
void addAtHead(int val) {
LinkedNode* newNode = new LinkedNode(val);
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;
}
// Add a node to the last side of the list
void addAtTail(int val) {
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(cur->next != nullptr){
cur = cur->next;
}
cur->next = newNode;
_size++;
}
// In the index Insert a new node before two nodes , for example index by 0, Then the newly inserted node is the new head node of the linked list .
// If index It's equal to the length of the list , The newly inserted node is the tail node of the linked list
// If index Greater than the length of the list , It returns null
void addAtIndex(int index, int val) {
if (index > _size) {
return;
}
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
// Delete the first index Nodes , If index Greater than or equal to the length of the linked list , direct return, Be careful index It's from 0 At the beginning
void deleteAtIndex(int index) {
if (index >= _size || index < 0) {
return;
}
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur ->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size--;
}
// Print linked list
void printLinkedList() {
LinkedNode* cur = _dummyHead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int _size;
LinkedNode* _dummyHead;
};206 Reverse a linked list
Double finger needling : Definition cur The pointer points to the head node ,pre The pointer is initialized to null, Record tmp = cur->next and cur->next = pre, to update tmp and cur.
ListNode* reverseList(ListNode* head){
ListNode* cur = head;
ListNode* pre = nullptr;
ListNode* tmp;
while(cur){
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
} 24 Two or two exchange nodes in a linked list
ListNode* swapPairs(ListNode* head) {
ListNode* dummyhead = new ListNode(0);
dummyhead->next = head;
ListNode* cur = dummyhead;
while(cur->next != nullptr && cur->next->next != nullptr){
ListNode* tmp1 = cur->next;
ListNode* tmp2 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = tmp1;
cur->next->next->next = tmp2;
cur = cur->next->next;
}
return dummyhead->next;
}19 Delete the last from the list n Nodes
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead;
//fast The pointer moves first n Step
while (n-- && fast->next != nullptr){
fast = fast->next;
}
// fast One more step ahead of time , Because we need to make slow Point to the previous node of the deleted node
fast = fast->next;
while(fast != nullptr){
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummyHead->next;
}142 Link list find ring
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while(fast!= NULL && fast->next != NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
ListNode* index1 = fast;
ListNode* index2 = head;
while(index1 != index2){
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}边栏推荐
- 【LeetCode】415. String addition
- Weekly recommended short video: talk about "meta universe" with a serious attitude
- 4275. Dijkstra sequence
- cookie加密 4 rpc方法确定cookie加密
- Numpy NP in numpy c_ And np r_ Explain in detail
- Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
- Time series data augmentation for deep learning: paper reading of a survey
- RISC-V架构下 FPU Context 的动态保存和恢复
- Depens:*** but it is not going to be installed
- Dynamic saving and recovery of FPU context under risc-v architecture
猜你喜欢

The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different

Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems

Kaformer personal notes
Depens:*** but it is not going to be installed

Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读

Squid代理服务器应用

Numpy numpy中的np.c_和np.r_详解

Huawei Router: GRE Technology

2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3

Data middle office: detailed explanation of technical architecture of data middle office
随机推荐
深入了解 border
threejs辉光通道01(UnrealBloomPass && layers)
从618看京东即时零售的野心
听说你还在花钱从网上买 PPT 模板?
Threejs glow channel 01 (unrealbroompass & layers)
支持向量机(SVC,NuSVC,LinearSVC)
牛客网 字符串变形
Qingcloud based "real estate integration" cloud solution
Installation of sophus package in slam14 lecture
Linux MySQL installation
每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
Mba-day25 best value problem - application problem
Data middle office: middle office practice and summary
Depens:*** but it is not going to be installed
leetcode--链表
MySQL data (Linux Environment) scheduled backup
浮点数表示法(总结自CS61C和CMU CSAPP)
[Niuke] length of the last word of HJ1 string
学习太极创客 — ESP8226 (十三)OTA
Remote connection of raspberry pie without display by VNC viewer