题目:

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的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【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 ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

  6. 【LeetCode每天一题】Reverse Linked List(链表反转)

    Reverse a singly linked list. Example:           Input: 1->2->3->4->5->NULL          ...

  7. 【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-> ...

  8. 【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 ...

  9. 【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 ...

  10. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

随机推荐

  1. 转 关于shell中if 语法结构的广泛误解

    转自 ttp://blog.csdn.net/security134/article/details/6742156 最近学习SHELL编程 这篇文章很好很重要.有些东西不能想当然.同时不是表面看起来 ...

  2. JBoss7.1.1远程无法访问

    一般情况下在JBoss7.1.1.Final版本中配置standalone/configuration/standalone.xml<interfaces>里面name为public的&l ...

  3. html锚点(mao dian)--特殊的超链接

    锚点(anchor):其实就是超链接的一种,一种特殊的超链接 普通的超链接,<a href="路径"></a> 是跳转到不同的页面 而锚点,<a hr ...

  4. 周记4——vue中动态添加图片无效、build上线后background-image路径问题

    又是一个周五,又一周要过去了...很开心,这周遇到了vue中的一个比较常见的坑,网上随便一搜就有了很多解决方案...“幸运”的是,我选了一个带坑的方案...所以我觉得有必要记录一下这个“坑中坑”... ...

  5. android添加系统(服务、应用)

    1. 添加系统服务 1.1 添加方式1:(不加入servicemanager统一管理的) 看Android6.0.1 init.rc解析中的第2章和第3章 方式1: 1). 写一个测试脚本test.s ...

  6. SIMD

    SIMD 概述 数据类型 静态方法:数学运算 静态方法:通道处理 静态方法:比较运算 静态方法:位运算 静态方法:数据类型转换 实例方法 实例:求平均值 概述 SIMD(发音/sim-dee/)是“S ...

  7. [转]JavaScript和html5 canvas生成圆形印章

    本文转自:http://www.cnblogs.com/dragondean/p/6013529.html 代码: function createSeal(id,company,name){ var ...

  8. springboot遇到的那些坑

    一.在springboot整合jsp时,程序中的所有配置都是正确的,但是在启动springboot后,访问无法找到jsp页面,报错404, 解决办法 二.在springboot整合jpa实现crud时 ...

  9. 九 ServerSocketChannel

    ServerSocketChannel是一个可以监听进来的TCP连接的通道,就像标准IO的ServerSocket一样.ServerSocketChannel类在java.nio.channels包中 ...

  10. 怎么让div显示一行,其余的隐藏。

    <style> div{ white-space: nowrap; text-overflow:ellipsis; text-overflow: ellipsis; overflow:hi ...