题目描述:

Reverse a singly linked list.

click to show more hints.

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

Subscribe to see which companies asked this question

分析:

注意这个数据结构,这是一个链表,要求颠倒顺序。考虑设置两个变量,来表示相邻的两个节点one和two,首先把头节点的next设置null,先取得three = two.next然后one= two.next。取得往后移动。one= two ,two= three

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null)
            return null;
        if (head != null && head.next == null)
            return head;
        ListNode fis = head;
        ListNode sed = head.next;
        fis.next = null;
        while (sed != null) {
            ListNode thd = sed.next;
            sed.next = fis;
            fis = sed;
            sed = thd;
        }
        return fis;
    }
}

leetcode之旅(9)-Reverse Linked List的更多相关文章

  1. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  2. LeetCode 206. 反转链表(Reverse Linked List) 16

    206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...

  3. [LeetCode&Python] Problem 206. Reverse Linked List

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

  4. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  5. LeetCode(92) 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- ...

  6. 【leetcode❤python】206. Reverse Linked List

    # Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         s ...

  7. leetcode 206 反转链表 Reverse Linked List

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

  8. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  9. [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-> ...

  10. [LeetCode]题解(python):092 Reverse Linked List II

    题目来源 https://leetcode.com/problems/reverse-linked-list-ii/ Reverse a linked list from position m to  ...

随机推荐

  1. FFmpeg源代码简单分析:avcodec_open2()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  2. struts extjs 3.3.1 读取JSON文件

    json文件和脚本代码: jsonSrc/jsonTxt1.json, { "personInfoList": [ { "id": 0, "name& ...

  3. Web开发的发展史

    英文出处:arunr.欢迎加入翻译小组. 导读:Arunr 把过去 15 年以来,Web开发从最初的纯 HTML 到 CGI.PHP\JSP\ASP.Ajax.Rails.NodeJS 这个过程简要地 ...

  4. 数据从oracle转换到mysql

    因为项目变更,需要把数据从oracle里转到mysql里. 第一个想法,自己写代码. 20分钟后,算了,还是找找工具吧. 第二步: 下了一个工具,二十分钟后,师兄发现,表的结构是倒完了,但是有的表数据 ...

  5. 【一天一道LeetCode】索引目录 ---C++实现

    [一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...

  6. 解决在onCreate()过程中获取View的width和Height为0的方法

    最近在看Android底层代码的view绘制原理的时候讲到一个很有意思的事情,也是我几年前刚开始学习Android开发的时候比较纳闷的一个问题,如果你不理解Android的底层绘制,请看我之前一片文章 ...

  7. javascript语法之Date对象与小案例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 看看腾讯是怎么做产品设计分析的 - 腾讯QQ音乐业务产品规划

  9. Leetcode_169_Majority Element

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42247887 Given an array of size ...

  10. android放大镜效果实现

    概述 我相信很多用过英语应用的同学都看多一个放大镜的效果,就是选中一段文字后,会有一个放大镜,这个究竟怎么实现的呢,我们今天来分析分析. 源码分析 public class ShaderView ex ...