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-> ...
随机推荐
- 15条变量&方法命名的最佳实践【转】
原文地址:15 Best Practices of Variable & Method Naming 不同的代码段采用不同的命名长度.通常来说,循环计数器(loop counters)采用1位 ...
- How to send Email using C#
try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail. ...
- Nginx 性能优化
1.安全优化:隐藏Nginx版本号,server_tokens off; 2.安全优化:更改掉默认的用户 user nginx; 3.性能优化: 根据硬件配置,调整nginx worker 进程数 ...
- String equals的技巧
把常量放到前面,可以避免null指针问题 System.out.print("".equals(null)); String abc = null; System.out.prin ...
- ASP.NET Web API实践系列04,通过Route等特性设置路由
ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...
- 线性判别分析(Linear Discriminant Analysis)转载
1. 问题 之前我们讨论的PCA.ICA也好,对样本数据来言,可以是没有类别标签y的.回想我们做回归时,如果特征太多,那么会产生不相关特征引入.过度拟合等问题.我们可以使用PCA来降维,但PCA没有将 ...
- MySQL使用位运算
通常 我们的数据表中 可能会包含各种状态属性, 例如 blog表中,我们需要有字段表示其是否公开,是否有设置密码,是否被管理员封锁,是否被置顶等等. 也会遇到在后期运维中,策划要求增加新的功能而造成你 ...
- Python之Rpyc模块
简介 rpyc (Remote Python Call)为分布式计算环境提供了优良的基础平台.使用rpyc编写c/s结构程序,完全不用考虑老式的socket编程,现在只用编写简单的3.5行代码即可完成 ...
- VT100字体
自从接触LINUX之后,VT100是我最喜欢的终端字体,当然它也是SecureCRT的默认字体.真实文件全名,VT100.FON 总共才44KB大小. 字体安装:直接放入C:\Windows\Fon ...
- 黄聪:wordpress前台自定义用户,调用wp_editor上传附件提示【抱歉,出于安全的考虑,不支持此文件类型】错误。
1.直接禁用文件类型检测,在wp-config.php文件中,添加这样一句代码define('ALLOW_UNFILTERED_UPLOADS', true); 2.在functions.php里面, ...