Reverse Linked List

描述

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL    
输出: 5->4->3->2->1->NULL

进阶:

你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

解析

设置三个节点precurnext

  • (1)每次查看cur节点是否为NULL,如果是,则结束循环,获得结果

  • (2)如果cur节点不是为NULL,则先设置临时变量nextcur的下一个节点

  • (3)让cur的下一个节点变成指向pre,而后pre移动curcur移动到next

  • (4)重复(1)(2)(3)

代码

迭代

/**
* 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) {
return head;
}
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}

递归

public static ListNode reverseLinkedList(ListNode head) {
if (head == null || head.next == null) {
return head;
} ListNode node = reverseLinkedList(head.next);
head.next.next = head;
head.next = null;
//比如1->2->3,第一次执行到这里:3->2->NULL
return node;
}

[LeetCode] 206. Reverse Linked List ☆(反转链表)的更多相关文章

  1. [leetcode]206. Reverse Linked List反转链表

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

  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倒置链表 C++

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

  4. 206 Reverse Linked List 反转链表

    反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...

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

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

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

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

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

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

  8. LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)

    翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...

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

    Reverse a singly linked list. click to show more hints. Subscribe to see which companies asked this ...

随机推荐

  1. Qt编写输入法V2019终极版

    一.前言 之前写过的V2018版本的输入法,本来已经很完善了,不打算更新升级了,最近有个朋友找我定制一个输入法,需要高仿一个苹果MAC电脑的输入法,MAC操作系统的审美无疑是相当棒的,于是乎直接拿以前 ...

  2. 使用MockMvc进行springboot调试(SpringbootTest)

    测试前关闭web项目.springboot启动程序WebApplication.class 笔者本地自定了端口SpringBootTest.WebEnvironment.DEFINED_PORT 代码 ...

  3. Tips for Conda

    管理环境 创建环境 基于 python3.6 创建一个名为test_py3的环境 conda create -n test_py3 python=3.6 基于 python2.7 创建一个名为test ...

  4. Node.js使用MySQL连接池示例

    下面是一个封装好的工具类: var fs = require('fs'); var mysql = require('mysql'); var pool = mysql.createPool({ ho ...

  5. 为什么每次登录要手动 source /etc/profile ...

    由于执行顺序如下,故追个查看以下文件,看看是不是 JAVA_HOME, PATH 等环境变量在后面的文件中被重写覆盖了. 1. /etc/profile2. /etc/environment3. ~/ ...

  6. Bilibili用户需求分析报告

    一.产品简介 哔哩哔哩(英文名称:bilibili,简称B站)是国内知名的弹幕视频分享站,也是国内领先的年轻人文化社区 二.用户需求分析 (一)目标用户 根据百度指数,bilibili的主要用户遍布沿 ...

  7. idea查看接口或类的所有方法

    第一种: 显示结果: 第二种: 点击左显示栏的Structure: 第三种:ctrl+f12,有的电脑可能需要加fn键

  8. java中this的使用

    java中的this随处可见,用法也多,现在整理有几点:this1.当全局变量跟局部变量重名时,表示使用全局变量(此时this指代本类对象)例有一类class A{    String name;   ...

  9. 一个简单的一个sql表遍历

    简单的一个sql表遍历 一般我们写储存过程或者其他sql语句的时候都会用到循环遍历数据,最常用的两种就是 1.游标 2.临时表+while 下面贴出示例代码 DECLARE @MinReLogID I ...

  10. 缓存利器之Ehcache

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点.是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器.另外Spring 提供了对缓存功能的抽象: ...