Reverse a singly linked list.

click to show more hints.

Subscribe to see which companies asked this question.

利用循环。

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

递归版


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

 

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

  1. Leetcode 206 Reverse Linked List 链表

    将单向链表反转 完成如图操作,依次进行即可 1 2 3 /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  2. 【easy】206. Reverse Linked List 链表反转

    链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...

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

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

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

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

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

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

  6. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

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

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

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

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

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

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

随机推荐

  1. js遍历商品编码

    function bathAuditGoods(state) { var selections = $("#deliveryGrid").datagrid('getSelectio ...

  2. 点击一个textView里的link导航至程序内可返回的自定义webView

    1,在AppDelegate.h里定义一个 id currentViewController; 在AppDelegate.m里 @implementation UIApplication (Priva ...

  3. FTP新建文件夹访问

    今天在远程服务器上添加了文件夹,本来还想着FTP打开看看,结果竟然发现没有这个文件夹访问 想了一下,感觉应该是FTP访问的文件设置,只有FTP设置了的文件夹才能有显示

  4. 【Java nio】 Blocking nio

    package com.slp.nio; import org.junit.Test; import java.io.File; import java.io.IOException; import ...

  5. type 、instanceof、in 和 hasOwnproperty

    typeof可以检测的类型有:string.number.boolean.undefined.不可以用typeof检测null typeof也可以用来检测function,但是在IE8及跟早的浏览器中 ...

  6. hdu 5318 The Goddess Of The Moon

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  7. hihocoder [Offer收割]编程练习赛14 投掷硬币

    题目2 : 投掷硬币 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi有一枚神奇的硬币.已知第i次投掷这枚硬币时,正面向上的概率是Pi. 现在小Hi想知道如果总共投 ...

  8. 三、微信小游戏开发 --- 小游戏API调用Platform

    微信小游戏API Platform主要是Egret用于来调用平台的SDK的. 在Egret中使用接口定义Platform. Egret项目中默认的platform值是DebugPlatform. 发布 ...

  9. linux 中 ll 命令显示 的大小 是什么单位的啊?

    ll显示的是字节,可以使用-h参数来提高文件大小的可读性,另外ll不是命令,是ls -l的别名 ls -al   是以字节单位显示文件或者文件夹大小: 字节b,千字节kb, 1G=1024M=1024 ...

  10. Windows Phone 页面切换动画

    1.首先引用Microsoft.Phone.Toolkit 2.将App.xaml.cs 中的 RootFrame = new PhoneApplicationFrame(); 改成RootFrame ...