Reverse a singly linked list.

  1. Input: 1->2->3->4->5->NULL
  2. Output: 5->4->3->2->1->NULL

Follow up:

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

题意:

如题

思路:

代码:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode reverseList(ListNode head) {
  11. ListNode cur = head;
  12. ListNode pre = null;
  13. while(cur!= null){
  14. ListNode temp = cur.next;
  15. cur.next = pre;
  16. cur = temp;
  17. }
  18. }
  19. }

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

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

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

  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. iOS-----使用AddressBook管理联系人

    使用AddressBook管理联系人 iPhone手机通常都是自带的Contacts应用,包括所有联系人的性(last name).名(first name).电话.E-mail地址.住址.生日等各种 ...

  2. train validation test

    http://stats.stackexchange.com/questions/19048/what-is-the-difference-between-test-set-and-validatio ...

  3. Git 将代码恢复到一个历史的版本

    Git 将代码恢复到一个历史的版本 要把代码回到某个历史版本 比如 test有两种方法 暴力的方式 如果你的仓库是自己在用(不影响别人),那么你可以使用 git reset --hard <ta ...

  4. memsql filesystem pipeline 试用

    一些功能类似drill ,比如s3,file ... 创建file pipeline 准备file mkdir -p /opt/db/ touch books.txt 内容如下: The Catche ...

  5. FastAdmin 环境变量 env 配置

    FastAdmin 环境变量 env 配置 目前 FastAdmin 支持环境变量 env 配置. 目前支持以下环境变量 app.debug app.trace database.type datab ...

  6. Phonegap 自定义插件

    一.PhoneGap中js与Java之间相互调用分为同步和异步两种方式 1.同步:js调用Java类的方法,然后Java类的方法直接返回一个值给js端 2.异步:js调用Java类的方法,Java类的 ...

  7. git连接报错:Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)

    在Linux上已经安装过git(自己搭建)了,本机(windows)想连接过去,通过git bash敲了下clone命令提示没权限: $ git clone git@111.11.111.11:cod ...

  8. (转)Eclipse新增安卓虚拟机

  9. kernel_task high cpu usage

    # Find the model $ system_profiler -detailLevel mini | grep "Model Identifier:" Model Iden ...

  10. SPOJ Count on a tree(主席树+LCA)

    一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to  ...