reverse list
public void reverse (){
Node end =null,p,q;
p=head; //p代表当前节点,end代表翻转后p后面的,q代表翻转前p后面的
while(p!=null){ //如果当前节点不为空
q=p.next; //q赋值为p后面的节点
p.next=end; //p指向p后面那个
end=p; //end后移一位
p=q; //p后移一位
}
head=end;
}
//上面代码使用的是非递归方式,这个问题也可以通过递归的方式解决。代码如下:
[java] view plain copy
public Node reverse(Node current)
{
if (current == null || current.next == null) return current;
Node nextNode = current.next;
current.next = null;
Node reverseRest = reverse(nextNode);
nextNode.next = current;
return reverseRest;
}
//递归的方法其实是非常巧的,它利用递归走到链表的末端,然后再更新每一个node的next 值 (代码倒数第二句)。 在上面的代码中, reverseRest 的值没有改变,为该链表的最后一个node,所以,反转后,我们可以得到新链表的head。
reverse list的更多相关文章
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- js sort() reverse()
数组中存在的两个方法:sort()和reverse() 直接用sort(),如下: ,,,,,,,,,,,]; console.log(array.sort());ps:[0, 1, 2, 2, 29 ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [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-> ...
随机推荐
- R(三): R包原理及安装
包(package)是多个函数的集合,常作为分享代码的基本单元,代码封装成包可以方便其他用户使用.越来越多的R包正在由世界上不同的人所创建并分发,这些分发的R包,可以从CRAN 或 github 上获 ...
- Android 打包签名 从生成keystore到完成签名 -- 转
首先,我们需要一个keystore,当然已经有了的话就不用这一步了:cmd下:进入到jdk的bin目录,这样的话,android.keystore文件就会生成在这个目录下,签名的时候我们需要这个文件C ...
- Angular学习(5)- 数组双向梆定+filter
示例: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 5< ...
- Cacti不显示图片(nan)
1.发现多半是SNMP的配置问题 被监控端安装snmp yum -y install net-snmp net-snmp-devel 被监控端安装配置snmp vim /etc/snmp/snmpd. ...
- HDU3530 子序列
题目大意:给出一串长度为n的整数串,求最长的一个连续子序列,满足该序列中最大的元素与最小的元素之差大于等于m, 并且小于等于k.n<=100000 分析:维护两个单调队列,一个递增的,维护最小值 ...
- Linux后台开发常用工具
内存分析工具valgrind valgrind辅助工具qcachegrind 可视化查看valgrind结果 淘宝DBA团队发布的监控脚本,使用perl开发,可以完成对linux系统和MySql相关指 ...
- POJ3318--Matrix Multiplication 随机化算法
Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? In ...
- linux内存文件系统之指南
内存文件系统使用及示例:ramdisk, ramfs, tmpfs 第一部分在Linux中可以将一部分内存mount为分区来使用,通常称之为RamDisk. RamDisk有三种实现方式: 第一种就是 ...
- MVC entity
1>MVC entity 1)Employee public string Id{get;private set;} public string Name{get;private set;} p ...
- Spark RDD简介与运行机制概述
RDD工作原理: 主要分为三部分:创建RDD对象,DAG调度器创建执行计划,Task调度器分配任务并调度Worker开始运行. SparkContext(RDD相关操作)→通过(提交作业)→(遍历RD ...