题目:

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. Word常用定义的变量

    unit U_WordConst;  interface {*******Word窗体状态************} const   wdWindowStateNormal = $00000000;  ...

  2. PyCharm+cmd中使用Anaconda 与 新建Python环境(Windows)

    PyCharm配置Anaconda Anaconda的安装在网上已经有了,这里主要讲之前已经安装了已经配置好Python环境变量以及PyCharm的情况下,使用Anaconda. 即在PyCharm中 ...

  3. selenium自动化测试用例需要关注的几点(二)

    注:原文地址 UI映射 一个UI映射是一种机制,它存储所有的定位器的测试套件在一个地方,方便修改UI元素的路径标识符或改变在AUT.测试脚本,然后使用UI地图定位以被测试的元件.基本上,UI地图是一个 ...

  4. Nodejs 实现windows后台运行

    首先需要到http://nssm.cc/download/?page=download 下载 nssm 下下来之后是压缩包形式的,解压之后 ctrl + R 进入cmd 命令行界面 在命令行模式下进入 ...

  5. unity 工具开发基础

    using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; usin ...

  6. java.lang.RuntimeException Unable to instantiate application Caused by: java

    参考:http://blog.csdn.net/xufazhong/article/details/71155528 dependencies { classpath 'com.android.too ...

  7. Eclipse 常见问题总结

    添加包 1.build path 直接添加 2.在windows-->preferences -> Java -> build path -> classpath variab ...

  8. tomcat绑定域名绑定端口及更换ROOT目录

    一.更换ROOT目录 tomcat默认网站目录为 webapps/ROOT ,那么我们如何改为自己的网站目录呢? 1.打开并编辑tomcat目录下的 conf/server.xml 大约在148行的位 ...

  9. linux修改用户名和密码

    linux修改用户名和密码 修改root密码:sudo passwd root 修改用户密码(如hadoop) sudo passwd hadoop 修改主机名:sudo vi /etc/hostna ...

  10. 静态代码块,构造代码块,main()

    静态代码块 随Class 加载而加载,为Class 作初始化: 在main() 之前加载: 只执行一次: 构造代码块 随对象的创建而加载,为对象作初始化 public class day04 { pu ...