当前位置: 首页 > java >正文

反转链表 - 简单

*************

C++

topic: 206. 反转链表 - 力扣(LeetCode)

*************

Give the topic an inspection.

It seems really easy. At very first, I think that I will use reverse cammand to kill this topic. But a few seconds later I found that I am strange about list link. So relearn the basic skills about the list link. 

Linked list is a structure. Linked list is used to store data which share the same type. As is known that memary is all to the computer, the linked list donot have to know the size of memory in advance. It has dynamic structure to store the data.

The basic structure of linked list is as follow, just keep it in ur mind.

struct ListNode 
{int val;            // 节点存储的值ListNode* next;     // 指向下一个节点的指针ListNode(int x) : val(x), next(nullptr) {}  // 构造函数
};

Make a linked list 1 -> 2 -> 3

ListNode* head = new ListNode(1);      // 头节点
head->next = new ListNode(2);         // 第二个节点
head->next->next = new ListNode(3);   // 第三个节点

go through linked list.

void printList(Node* head) 
{Node* current = head;while (current != nullptr) {current = current->next;}
}

add 0 in the very begin:  0 ->1 -> 2 -> 3

ListNode* newNode = new ListNode(0);
newNode->next = head;  // 新节点指向原头节点
head = newNode;        // 更新头节点
// 链表变为 0 → 1 → 2 → 3

delete 2: 0 ->1 -> 3

ListNode* curr = head;
while (curr->next != nullptr && curr->next->val != 2) 
{curr = curr->next;
}
if (curr->next != nullptr) 
{ListNode* temp = curr->next;curr->next = curr->next->next;  // 跳过待删除节点delete temp;                    // 释放内存
}
// 链表变为 0 → 1 → 3

maybe you donot understand why delete the selected node is so camplex, me either. Donot worry about it. I will show me something interesting.

ListNode* head = new ListNode(1);      // 节点1 (val=1)
head->next = new ListNode(2);          // 节点2 (val=2)
head->next->next = new ListNode(3);    // 节点3 (val=3)

head → [1|next] → [2|next] → [3|next] → nullptr

linked list always has head and nullptr, like a zip, head is head, nullptr is end.

if I want to delete ListNode(2), I should find ListNode(1) first.

head → [1|next] → [2|next] → [3|next] → nullptr

// 3. 遍历链表,找到待删除节点的前驱节点
ListNode* curr = head;
while (curr->next != nullptr && curr->next->val != 2) 
{curr = curr->next;  // 移动到下一个节点
}

Once ListNode(1) is found, delete 2

// 4. 如果找到待删除节点(curr->next 是节点2)
if (curr->next != nullptr) 
{ListNode* temp = curr->next;      // 临时保存节点2的地址curr->next = curr->next->next;    // 让节点1直接指向节点3delete temp;                      // 释放节点2的内存
}

head → [1|next] → [3|next] → nullptr.

Maybe I will forget how to use the linked list few days later but donot worry, I will learn angain.

Back to the topic.

reverse the linked list, I think make a new linked list, head == nullptr.

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode *previous = nullptr; // 前一个节点,初始化为链表尾部ListNode *current  = head; // 当前节点,初始化为链表头}
};

reverse\

class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode *previous = nullptr; // 前一个节点,初始化为链表尾部ListNode *current  = head; // 当前节点,初始化为链表头while (current != nullptr){ListNode *nextTemporary = current->next; // 临时变量current->next           = previous;previous                = current;current                 = nextTemporary;}return previous;}
};

for example.

head -> 1 -> 2 -> 3 -> nullptr

prev = nullptr
curr = 1 -> 2 -> 3 -> nullptr

第一次循环:

  1. nextTemp = curr->next → 保存节点2的地址
  2. curr->next = prev → 节点1的next指向nullptr
  3. prev = curr → prev指向节点1
  4. curr = nextTemp → curr指向节点2

prev = 1 -> nullptr
curr = 2 -> 3 -> nullptr

第二次循环:

  1. nextTemp = curr->next → 保存节点3的地址
  2. curr->next = prev → 节点2的next指向节点1
  3. prev = curr → prev指向节点2
  4. curr = nextTemp → curr指向节点3

prev = 2 -> 1 -> nullptr
curr = 3 -> nullptr

第三次循环

  1. nextTemp = curr->next → 保存nullptr
  2. curr->next = prev → 节点3的next指向节点2
  3. prev = curr → prev指向节点3
  4. curr = nextTemp → curr指向nullptr

prev = 3 -> 2 -> 1 -> nullptr
curr = nullptr

#

http://www.xdnf.cn/news/5540.html

相关文章:

  • SET NX互斥功能的实现原理
  • 【AI大语言模型本质分析框架】
  • 在Mac环境下搭建Docker环境的全攻略
  • 技术视界 | 青龙机器人训练地形详解(四):复杂地形精讲之斜坡
  • 因子分析基础指南:原理、步骤与地球化学数据分析应用解析
  • 数据出境的安全合规思考
  • 17.three官方示例+编辑器+AI快速学习webgl_buffergeometry_lines
  • LabVIEW中算法开发的系统化解决方案与优化
  • 如何查看电脑处理器配置 电脑处理器查看方法
  • CSP-J普及组第一轮真题单选题专项训练(一)
  • 欧姆龙CJ/CP系列PLC串口转网口模块:工业通信的智能桥梁
  • 矩阵置零算法讲解
  • 跨时钟域(CDC,clock domain crossing)信号处理
  • 新型.NET恶意软件“PupkinStealer“窃取浏览器凭证并通过Telegram外传
  • window 显示驱动开发-指定 DMA 缓冲区的段
  • .NET 8 + Angular WebSocket 高并发性能优化
  • Matlab 模糊控制平行侧边自动泊车
  • MySQL之GET_JSON_OBJECT函数
  • Express知识框架
  • Linux常用命令详解(下):打包压缩、文本编辑与查找命令
  • C++GO语言微服务之Dockerfile docker-compose
  • 手机换地方ip地址会变化吗?深入解析
  • CSS3 伪元素(Pseudo-elements)大全
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(二十二)
  • 【25软考网工】第六章(4)VPN虚拟专用网 L2TP、PPTP、PPP认证方式;IPSec、GRE
  • USB传输模式
  • 大语言模型强化学习双强:OpenRLHF与verl技术解析
  • Golang空接口的用途详解
  • pnpm使用报错
  • TWASandGWAS中GBS filtering and GWAS(1)