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. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  2. 第十五章 单点登录——《跟我学Shiro》

    目录贴:跟我学Shiro目录贴 Shiro 1.2开始提供了Jasig CAS单点登录的支持,单点登录主要用于多系统集成,即在多个系统中,用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个 ...

  3. page工具类

    工具类 /** * @Title: PageUtil.java * @Package * @Description: TODO(用一句话描述该文件做什么) * @author licy * @date ...

  4. TCP Socket + UDP Socket

    小例子:http://soft.yesky.com/238/2035738.shtml 服务器程序: #include <iostream> #include <WinSock2.h ...

  5. 49.Django起步学习

    django起步 django安装 pip install django==2.0.4(版本号) pip install django 默认安装最新版本 创建项目 django-admin start ...

  6. clog就用clog的后缀名

    /tmp/log/shuanggou.clog /tmp/log/shuanggou.log /tmp/log/shuanggou_success.log /tmp/log/shuanggou_err ...

  7. JAVA MAC 比较大小

    2个MAC String mac_1="AAAAAAAAAAAA"; String mac_1="AAAAAAAAAAAB"; 方法一: int a = 0; ...

  8. Maven打包成可执行JAR(带依赖包)

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  9. 基于聚类K-Means方法实现图像分割

    ”“”K-Means to realize Image segmentation “”“ import numpy as np import PIL.Image as image from sklea ...

  10. ubuntu 上不了网,解决方案之一

    每个人的情况可能不同,我的情况是由于强制关机网卡坏了,网络没有自动分配ip,ens33网卡没有ip,这时得手动启动命令 sudo dhclient 来自动获取ip地址.这里要感谢这篇博客,让我意识到自 ...