206. 反转链表

206. Reverse Linked List

题目描述

反转一个单链表。

每日一算法2019/5/19Day 16LeetCode206. Reverse Linked List

示例:

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

进阶:

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

Java 实现

ListNode 类

class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
} @Override
public String toString() {
return val + "->" + next;
}
}

Iterative Solution

class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
}

Recursive Solution

class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
}

测试类

public class Test {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode a = new ListNode(2);
ListNode b = new ListNode(3);
ListNode c = new ListNode(4);
ListNode d = new ListNode(5);
head.next = a;
a.next = b;
b.next = c;
c.next = d; System.out.println(head);
System.out.println(new Solution().reverseList(head));
}
}

运行结果

Iterative Solution

1->2->3->4->5->null
5->4->3->2->1->null

Recursive Solution

1->2->3->4->5->null
5->4->3->2->1->null

相似题目

参考资料

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

  1. leetcode 206 反转链表 Reverse Linked List

    C++解法一:迭代法,使用前驱指针pre,当前指针cur,临时后继指针nxt: /** * Definition for singly-linked list. * struct ListNode { ...

  2. 每天一道面试题LeetCode 206 -- 反转链表

    LeetCode206 反转链表 思路 代码 # # @lc app=leetcode.cn id=206 lang=python3 # # [206] 反转链表 # # https://leetco ...

  3. leetCode:206 反转链表

    206. 反转链表 题目:反转一个单链表. 进阶:链表可以迭代或递归地反转.你能否两个都实现一遍? 非递归代码: class Solution { public ListNode reverseLis ...

  4. Java实现 LeetCode 206 反转链表

    206. 反转链表 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL ...

  5. leetcode 206. 反转链表 及 92. 反转链表 II

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

  6. LeetCode 206:反转链表 Reverse Linked List

    反转一个单链表. Reverse a singly linked list. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3- ...

  7. [Swift]LeetCode206. 反转链表 | Reverse Linked List

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

  8. 反转链表 Reverse Linked List

    2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...

  9. LeetCode 206.反转链表(Python3)

    题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶:你可 ...

随机推荐

  1. Deepin Create/Delete Folder refresh

    Did u have a problem whth the deepin file manager,Everthime I create/delete a Folder of File i have ...

  2. Ubuntu不能连接网络

    我的问题是在选择桥接模式下的界面名称选择错误,在windows中我的Intel7260显示未连接,所以更改为Controller之后好用了,折磨了我大半天.

  3. 在mybtis的映射文件中判断集合大小

    <if test="groupIds != null and groupIds.size>0"> and (group_id in<foreach coll ...

  4. arcpy 重分类

    arcpy.gp.Reclassify_sa("dem.tif","Value","0 2000 1;2000 2100 2;2100 2500 3; ...

  5. unity手机游戏应用程序调试控制台Lunar Mobile Console - PRO 1.5.5

    unity手机游戏应用程序调试控制台Lunar Mobile Console - PRO 1.5.5 High-performance Unity iOS/Android console built ...

  6. iview3 版本 升级

    Button 废弃 type ghost,原先的 default 样式有改变. Icon 的图标升级至 ionicons 3.0 图标,图标名称有改变. Breadcrumb 废弃 href 属性. ...

  7. Flink 之 Data Source

    Data Sources 是什么呢?就字面意思其实就可以知道:数据来源. Flink 做为一款流式计算框架,它可用来做批处理,即处理静态的数据集.历史的数据集: 也可以用来做流处理,即实时的处理些实时 ...

  8. https://suchprogramming.com/epoll-in-3-easy-steps/

    https://suchprogramming.com/epoll-in-3-easy-steps/ https://www.quora.com/What-are-the-key-difference ...

  9. Shell脚本自动重启Java服务

    话不多说直接上代码: cd /home/javaProduct/if [ -d '/home/javaProduct/lib_new/' ]; thenecho 'Has New Lib!'echo ...

  10. Vue CLI3和Vue CLI2环境搭建

    关于 Vue CLI 旧版本的安装以及创建项目 1.搭建 vue 的开发环境 ,安装 vue 的脚手架工具 官方命令行工具 npm install --global vue-cli / cnpm in ...