package LinedList;

public class ReverseASinglyLinkedList {
//解法一:迭代。
public ListNode reverseList(ListNode head) {
ListNode previous = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next;
current.next = previous;
previous = current;
current = next;
}
return current;
}
/**
* 解法二:递归
* 递归的思路其实和迭代一样,就是处理两个节点,让它们之间的指针反转。
* 只是,迭代是从头到尾依次处理,需要一个额外的指针来指向下一个节点。
* 而递归是从尾到头,回溯的时候会有当前的节点,所以不需要额外的指针。
* 还需要注意的是:p是反转后的整个链表的头节点,它第一次找到后,就没有作任何处理。
* 而不是每次要处理(反转)的那个节点。
*/
public ListNode reverseList2(ListNode head) {
if (head==null||head.next==null)
return head;
ListNode p=reverseList2(head.next);
head.next.next=head;
head.next=null;
return p;
}
}

206--Reverse A Singly Linked List的更多相关文章

  1. LeetCode 206 Reverse a singly linked list.

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

  2. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  3. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

  4. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  7. Java [Leetcode 206]Reverse Linked List

    题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...

  8. 【LeetCode】206. Reverse Linked List

    题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...

  9. 【一天一道LeetCode】#206. Reverse Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

  10. LeetCode 206. Reverse Linked List倒置链表 C++

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

随机推荐

  1. 基于Distiller的模型压缩工具简介

    Reference: https://github.com/NervanaSystems/distiller https://nervanasystems.github.io/distiller/in ...

  2. 树莓派autossh反向隧道

    本来我是将树莓派连接到路由器,从而在电脑端通过IP访问.远在局域网之外的队友怎么访问呢? ssh反向隧道 它的原理比较简单: 树莓派主动向某公网服务器建立ssh连接,并请求公网服务器开启一个额外的SS ...

  3. bikemanager

    项目特色 前言的技术栈  健全的架构  丰富的UI组件  共享单车项目 掌握react全家桶 掌握地图和react集成技能 掌握前端图标开发技巧 掌握antd UI框架 前端后台架构设计,公共机制封装 ...

  4. 复杂模拟 | 1017 模拟N个顾客M个柜台进行排队

    #include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...

  5. 【转】Java 泛型

    转载:https://www.cnblogs.com/lwbqqyumidi/p/3837629.html. 一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public ...

  6. 第09组 Beta冲刺(5/5)

    队名:观光队 链接 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 学习 展示GitHub当日代码/文档签入记录 无 接下来的计划 无 还剩下哪些任务 s 答辩 遇到 ...

  7. oracle--JOB任务

    1.创建一张测试表 -- Create table create table A8 ( a1 VARCHAR2(500) ) tablespace TT1 pctfree 10 initrans 1 ...

  8. Elasticsearch由浅入深(十一)内核原理

    倒排索引组成结构以及索引不可变原因 对于倒排索引是非常适合用来进行搜索的它的结构:(1)包含这个关键词的document list(2)包含这个关键词的所有document的数量:IDF(invers ...

  9. go-gin-api 路由中间件 - Jaeger 链路追踪

    概述 首先同步下项目概况: 上篇文章分享了,路由中间件 - Jaeger 链路追踪(理论篇). 这篇文章咱们分享:路由中间件 - Jaeger 链路追踪(实战篇). 说实话,这篇文章确实让大家久等了, ...

  10. Makefile的简洁模版

    博客地址:http://www.cnblogs.com/zengjianrong/p/4184854.html 为了方便编译零碎的测试代码,在代码的存放目录编辑了一个Makefile,添加新代码文件后 ...