单链表反转的递归实现(Reversing a Linked List in Java, recursively)
转自Reversing a Linked List in Java, recursively
There's code in one reply that spells it out, but you might find it easier to start from the bottom up, by asking and answering tiny questions (this is the approach in The Little Lisper):
- What is the reverse of null (the empty list)? null.
- What is the reverse of a one element list? the element.
- What is the reverse of an n element list? the reverse of the second element on followed by the first element.
public ListNode Reverse(ListNode list)
{
if (list == null) return null; // first question if (list.next == null) return list; // second question // third question - in Lisp this is easy, but we don't have cons
// so we grab the second element (which will be the last after we reverse it) ListNode secondElem = list.next; // bug fix - need to unlink list from the rest or you will get a cycle
list.next = null; // then we reverse everything from the second element on
ListNode reverseRest = Reverse(secondElem); // then we join the two lists
secondElem.Next = list; return reverseRest;
单链表反转的递归实现(Reversing a Linked List in Java, recursively)的更多相关文章
- 单链表反转(递归和非递归) (Java)
链表定义 class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } 非递归实现很简单,只需要遍历一遍链表,在遍历过 ...
- java实现单链表反转(倒置)
据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; ...
- 单链表反转java代码
据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. public class Node { int index; Nod ...
- Java单链表反转 详细过程
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...
- java单链表反转
今天做leetcode,遇到了单链表反转.研究了半天还搞的不是太懂,先做个笔记吧 参考:http://blog.csdn.net/guyuealian/article/details/51119499 ...
- 经典算法(三) 单链表 反转 & 是否相交/成环 & 求交点 等
参考文章: 判断链表是否相交:http://treemanfm.iteye.com/blog/2044196 一.单链表反转 链表节点 public class Node { private int ...
- java 单链表反转
最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...
- 单链表反转的原理和python代码实现
链表是一种基础的数据结构,也是算法学习的重中之重.其中单链表反转是一个经常会被考察到的知识点. 单链表反转是将一个给定顺序的单链表通过算法转为逆序排列,尽管听起来很简单,但要通过算法实现也并不是非常容 ...
- Java单链表反转图文详解
Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...
随机推荐
- JQuery Mobile页面加载处理
在弄移动Web时采用了JQueryMobile框架. 奇怪的是 在使用页面加载 时 事件无效 我尝试了两种方法: $(document).ready(function(){ //do events } ...
- get方法与post方法的使用
使用get方法获取页面的form内容 新建一个getform.html <html> <head> <title>Using Http Get Method< ...
- java 下载文件功能代码例子
public static void down(HttpServletRequest request, HttpServletResponse response) throws Exceptio ...
- [Bootstrap]全局样式(四)
按钮 1.基本类.btn {display/padding/margin-bottom/font-size/border-radius/border} 作用于< a:role:button &g ...
- JAVA解析xml的五种方式比较
1)DOM解析 DOM是html和xml的应用程序接口(API),以层次结构(类似于树型)来组织节点和信息片段,映射XML文档的结构,允许获取 和操作文档的任意部分,是W3C的官方标准 [优点] ① ...
- [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组
1.数组基础 1.什么是数组: 同一类型数据的集合,就是一个容器. 2.数组的好处: 可以自动为数组中的元素从零开始编号,方便操作这些数据. 3.格式: (一 ...
- ECMAScript 5.1 Edition DOC 学习笔记
1.类型 string number object boolean null undefined symbol (es6) attention : (typeof null) 值为 'object', ...
- 使用JPA TOOLS从数据库生成Entity文件
数据库设计好后,需要生成对应的Entity文件,这是一项不怎么需要动脑筋的工作,最好的方法是交给工具完成,手工操作很容易写错或者遗漏.这里选择的工具就是JPA TOOLS. (1)先选中工程,查看右键 ...
- jQuery获取同级元素
next()相邻下一个同级元素 prev()相邻上一个同级元素 siblings()所有同级元素 $("#id").next(); $("#id").prev( ...
- 快速解码base64和utf-8的ASCII编码和URL解码
看论坛上总是有人发乱七八糟的文字,根本看不懂,用下面的方法解密一下. 只要有浏览器的开发者工具就行了. UTF-16解码 console.log("\u5475\u5475") U ...