【Leetcode】【Easy】Reverse Linked List
题目:
Reverse a singly linked list.
解题:
反转单链表,不再多介绍了.
如果会“先条件->定参数->确定不变式->验证后条件”的思维方法,一定会bug free.
/**
* 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* newhead = head;
ListNode* curNode = NULL;
head = head->next;
newhead->next = NULL; while (head) {
curNode = head;
head = head->next;
curNode->next = newhead;
newhead = curNode;
} return newhead;
}
};
写完初始版本后,再考虑如何去除循环前冗余的赋值,洁简代码:
/**
* 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* newhead = NULL;
ListNode* nextNode = NULL; while (head) {
nextNode = head->next;
head->next = newhead;
newhead = head;
head = nextNode;
} return newhead;
}
};
【Leetcode】【Easy】Reverse Linked List的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode 92. 反转链表 II(Reverse Linked List II)
92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...
- 【LeetCode每天一题】Reverse Linked List(链表反转)
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...
- 【leetcode刷题笔记】Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【leetcode刷题笔记】Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
随机推荐
- VUE安装步骤
项目构建 项目推荐直接使用 Vue 官方提供的脚手架(Vue-cli),所以第一步首先是安装脚手架.在命令行或者 IDE 的 Terminal 窗口中输入以下命令即可自动安装: npm install ...
- Protrator 官网和下载工具稍慢 , 但能使用. Angular CLI 内置 官方推荐 TS的 demo 不明显 , 而且依赖无法安装
npm uninstall -g protractor && cnpm install -g protractor && protractor --version 复 ...
- Oracle pl/sql编程值控制结构
以下测试案例均来自于scott方案,运行前请确保该方案解锁. 1.条件分支状语 pl/sql中提供了三种条件分支状语,分别是: if then if then else if then ...
- VS2008默认的字体居然是 新宋体
本人还是觉得 C#就是要这样看着舒服
- Scrapy框架学习(一)Scrapy框架介绍
Scrapy框架的架构图如上. Scrapy中的数据流由引擎控制,数据流的过程如下: 1.Engine打开一个网站,找到处理该网站的Spider,并向该Spider请求第一个要爬取得URL. 2.En ...
- OutSystems学习笔记。
ew job and new software, new challenge as well. OutSystems这软件挺好上手的.虽然没有中文文档,但英文文档超级详细,堪称傻瓜版SOP 照着步骤写 ...
- [转]How to Use Web API OData to Build an OData V4 Service without Entity Framework
本文转自:http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity- ...
- Lucence学习之一:全文检索的基本原理
本文转载自: http://www.cnblogs.com/forfuture1978/archive/2009/12/14/1623594.html 一.总论 根据http://lucene.ap ...
- golang学习之go简单博客应用
先说说golang的语法吧,个人觉得有以下特点: 简洁,不管是变量.方法声明,还是代码编写,均十分简洁,效率也比较高 非纯粹面向对象,但是go的struct类似c的struct,go的结构体还可以进行 ...
- Java数组逆序存储
package review01; import java.util.Arrays; public class review01 { public static void main(String[] ...