Question

Reverse a singly linked list.

Solution 1 -- Iterative

Remember to set head.next = null or it will report "memory limit exceeds" error.

 /**
* 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) {
ListNode current = head, prev = head, post = head;
if (current == null || current.next == null)
return current;
current = current.next;
head.next = null;
while (current.next != null) {
post = current.next;
current.next = prev;
prev = current;
current = post;
}
current.next = prev;
return current;
}
}

Solution 2 -- Recursive

We can also use recursion to solve this problem.

 /**
* 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 || head.next == null)
return head;
ListNode second = head.next;
head.next = null;
ListNode newHead = reverseList(second);
second.next = head;
return newHead;
}
}

Reverse Linked List 解答的更多相关文章

  1. Reverse Linked List II 解答

    Question Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Giv ...

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

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

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

  4. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

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

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

  6. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  7. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  8. 14. Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  9. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

随机推荐

  1. 自定义的string类

    头文件Hi_String.h #include<iostream> #include<string.h> using namespace std; class Hi_Strin ...

  2. Linux常用命令及使用技巧

    本文重点讲述Linux命令的使用,命令是学习Linux必须熟练掌握的一个部分.Linux下的命令大概有600个,而常用的命令其实只有80个左右,这些常用的命令是需要灵活掌握的.虽然Linux的各个发行 ...

  3. IOS 创建一张有颜色的UIImage

    #import <UIKit/UIKit.h> @interface UIImage (ImageWithColor) + (UIImage *)imageWithColor:(UICol ...

  4. python学习之路-9 socket网络编程

    socket基础 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...

  5. JS 数组扩展函数--求起始项到终止项和

    Array.prototype.sum= function(l,r){ l=l==undefined ? 0 : l; r=r==undefined ? this.length - 1 : r; va ...

  6. [置顶] myEclipse8.5或者eclipse手工安装jd插件(myEclipse8.5或eclipse内直接查看.class文件,jd反编译工具)

    myEclipse8.5或eclipse下手工安装jd-gui反编译软件 下载jdeclipse_update_site.zip网址是(http://dldx.csdn.net/fd.php?i=32 ...

  7. 2. QT窗体间值的传递

    一.主窗体与子窗体传参 方法有很多,这里介绍一种通过重载子窗体的构造函数实现主窗体参数传入到子窗体,并通过QT信号和槽的机制实现子窗口到主窗口值的传递. 主和子窗体的设置如下: 主要实现功能为: 1 ...

  8. [Python学习笔记][第八章Python异常处理结构与程序调试]

    1/30 第八章Python异常处理结构与程序调试 异常处理 try-except结构 try: try块 except Exception: except块 try-except-else结构 tr ...

  9. linux磁盘管理、新增磁盘、分区、挂载

    1. du -sh 查看目录.文件总大小 -a:全部文件与目录大小都列出来.如果不加任何选项和参数只列出目录(包含子目录)大小. -c:最后加总2. df -h 查看磁盘使用量3. lsblk 查看系 ...

  10. Pull生成&解析

    开篇注意,由于解析有可能有大文件非常耗时,建议另开一个线程解析也可以不开具体视情况而定     Pull生成 1.通过xml获得序列化的实例 XmlSerializer nxs = Xml.newSe ...