Leetcode 206 Reverse Linked List

Leetcode 206 Reverse Linked List

文章目录

1. 题意描述

Reverse a singly linked list.反转链表是一个基础问题,但也是一个很重要的问题。

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

2. 思路解析一

通过prev、cur、nex三个指针,prev指向的是前一个节点,cur指向的是当前节点,nex指向的是当前节点的后一个节点,其中prev和cur是为了实现节点指向的反转。其中nex可作为局部变量,也可以作为全局变量。如果nex作为局部变量,它仅仅是是为了使cur不断后移(2020.3.4复习),它的走向为nex = cur.next。nex为全局变量,并且它的走向为nex = nex.next。

2.1 代码实现一,nex为局部变量

2.1.1 C++代码

class Solution { public: ListNode* reverseList(ListNode* head) { if (head == NULL || head->next == NULL) { return head; } ListNode* prev = NULL; ListNode* cur = head; while (cur) { ListNode* nex = cur->next; cur->next = prev; prev = cur; cur = nex; } return prev; } }; 

学习小窍门:对角线法则

2.1.2 Java代码

class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode prev = null; ListNode cur = head; while (cur != null) { ListNode nex = cur.next; cur.next = prev; prev = cur; cur = nex; } return prev; } } 

2.1.3 Python代码

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None or head.next == None: return head prev = None cur = head while cur: nex = cur.next cur.next = prev prev = cur cur = nex return prev 

2.2 代码实现二,nex为全局变量

错误代码为:

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { if (head == NULL || head->next == NULL) { return head; } ListNode* prev = NULL; ListNode* cur = head; ListNode* nex = head->next; while (cur) { cur->next = prev; prev = cur; cur = nex; nex = nex->next; } return prev; } }; 

思考一下为什么是错的?’

2.2.1 c++代码

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { if (head == NULL || head->next == NULL) { return head; } ListNode* prev = NULL; ListNode* cur = head; ListNode* nex = head->next; while (cur) { cur->next = prev; prev = cur; cur = nex; if (nex == NULL) { return prev; } nex = nex->next; } return prev; } }; 

2.2.2 Java代码

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode prev = null; ListNode cur = head; ListNode nex = head.next; while(cur != null) { cur.next = prev; prev = cur; cur = nex; if (nex == null) { return prev; } nex = nex.next; } return prev; } } 

2.2.3 Python代码

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None or head.next == None: return head prev = None cur = head nex = head.next while cur: cur.next = prev prev = cur cur = nex if not nex: return prev nex = nex.next return prev 

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.