题目:

Reverse a singly linked list.

Example:

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

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

分析:

分别用迭代和递归来实现。

迭代就是新建一个newhead节点,遍历原链表,将每一个node接到newhead,注意保存head->next用来更新head。

递归则是利用函数先走到链表尾端,依次更新每一个node的next,最后返回newhead。

程序:

//iteratively
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *newhead = NULL;
while(head){
ListNode *p = head->next;
head->next = newhead;
newhead = head;
head = p;
}
return newhead;
}
};
//recursively
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL){
return head;
}
ListNode* newhead = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return newhead;
}
};

LeetCode 206. Reverse Linked List(C++)的更多相关文章

  1. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  2. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  3. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  4. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  5. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

  6. LeetCode 206. Reverse Linked List (倒转链表)

    Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...

  7. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  8. Java [Leetcode 206]Reverse Linked List

    题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...

  9. LeetCode 206. Reverse Linked List倒置链表 C++

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

  10. LeetCode 206 Reverse Linked List 解题报告

    题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...

随机推荐

  1. 利用纯JS和HTML Canvas生成随机迷宫过程中产生的有趣的事情

    上效果图: #先看生成随机迷宫的代码吧↓ <html> <head> <title>生成随机迷宫v1.0</title> </head> & ...

  2. node.js的http模块创建基本Web服务器

    首先下载node.js模块.终端执行命令 npm i node -g 引入http核心模块 const http =require('http') 引入文件系统模块 const fs =require ...

  3. 基于 HTML5 Canvas 的 3D 渲染引擎构建生产管控系统

    前言 大家好,老郑我又回来了.这一期为大家带来一个非常好玩的 demo,我们制作一套自己的 3D 管道控制系统,运用了( http://www.hightopo.com )HT 的 Graph3dVi ...

  4. Windows8系统下设置Mongodb开机启动

    1. 官网下载安装 MongoDB https://www.mongodb.com/ 2. 环境变量设置 把 mongod.exe 所在路径加入到环境变量的PATH, 我这里安装的路径是 D:\db\ ...

  5. CDH升级 5.7.5 --> 5.13.3(tar包方式)

    博客园首发,转载请注明出处:https://www.cnblogs.com/tzxxh/p/9123231.html 一.准备 1.关闭cdh中的服务 hdfs.yarn等所有服务:关闭 cm-ser ...

  6. ubuntu下python在pycharm环境下安装setuptools和pip,和distutils.core

    python安装好后,我们用pycharm安装所需的第三方模块时,出现“Python packaging tools not found. install packaging tools”点击安装输完 ...

  7. Git 命令及使用经验

    手册中的基本命令: CONFIGURE TOOLING Configure user information for all local repositories $ git config --glo ...

  8. Java使用POI导出excel(下)——实例与小技巧

    [更新]:thinkgem的导出工具类: /** * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesit ...

  9. 20155325《Java程序设计》实验一(Java开发环境的熟悉)实验报告

    实验一Java开发环境的熟悉-1 提交 Linux或Window或macOS中命令行下运行Java的截图 运行结果包含自己学号等证明自己身份的信息 实验一Java开发环境的熟悉-2 提交 Linux或 ...

  10. Caliburn.Micro - Getting Started - Introduction

    Caliburn.Micro Xaml made easy Introduction When my “Build Your Own MVVM Framework” talk was chosen f ...