LeetCode_206. Reverse Linked List
206. Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
package leetcode.easy; /**
* Definition for singly-linked list. public class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; } }
*/
public class ReverseLinkedList {
private static void print(ListNode head) {
if (head == null) {
return;
}
while (head != null) {
System.out.print(head.val);
if (head.next != null) {
System.out.print("->");
}
head = head.next;
}
System.out.println("->NULL");
} public ListNode reverseList(ListNode head) {
ListNode nextNode = null;
ListNode curr = head;
while (curr != null) {
ListNode tempNext = curr.next;
curr.next = nextNode;
nextNode = curr;
curr = tempNext;
}
return nextNode;
} @org.junit.Test
public void test() {
ListNode ln1 = new ListNode(1);
ListNode ln2 = new ListNode(2);
ListNode ln3 = new ListNode(3);
ListNode ln4 = new ListNode(4);
ListNode ln5 = new ListNode(5);
ln1.next = ln2;
ln2.next = ln3;
ln3.next = ln4;
ln4.next = ln5;
ln5.next = null;
print(ln1);
print(reverseList(ln1));
}
}
LeetCode_206. Reverse Linked List的更多相关文章
- [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 Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- Leetcode-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- 【leetcode】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- 14. Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
- Reverse Linked List | & ||
Reverse Linked List I Reverse a linked list. Example For linked list 1->2->3, the reversed lin ...
随机推荐
- 动态加载 ShellCode绕过杀软
反病毒解决方案用于检测恶意文件,并且通常使用静态分析技术来区分二进制文件的好坏.如果是恶意文件本身包含恶意内容(ShellCode),那么依靠静态分析技术会非常有效,但如果攻击者使用轻量级的stage ...
- 08 node.js 的使用
创建包 目录结构 cmd cd 到当前目录: \ 执行 npm init //创建一个包 1 2. 3. 4.包的安装 npm install jquery --save npm install ...
- HDFS的读机制
HDFS的读机制: 1.初始化FileSystem ,客户端调用FileSystem 中的open方法打开文件. 2.FileSystem 调用远程RPC服务,获取namenode上的文件的数据块信息 ...
- kindle touch 5.1.2 update your kindle 灰色 解决办法
要出差了,于是把抽屉里的老Kindle Touch拿出来想升个级,baidu说多看费电,果断卸了用原生. 但是原生里面升级选项“update your kindle”是灰色的,没法点,怎么办? 试了半 ...
- linux线程操作
初始化条件变量 int pthread_cond_init(pthread_cond_t *cv,pthread_cond_attr *cattr); 函数返回值:返回0表示成功,返回其他表示失败. ...
- RabbitMQ入门学习系列(四) 发布订阅模式
发布订阅模式 什么时发布订阅模式 把消息发送给多个订阅者.也就是有多个消费端都完整的接收生产者的消息 换句话说 把消息广播给多个消费者 消息模型的核心 RabbitMQ不发送消息给队列,生产者也不知道 ...
- Windows下基于IIS服务的SSL服务器的配置
Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...
- linux 9.故障修复和网络配置
一.linux系统故障修复 1.不知道root密码的前提下 破解root密码 服务器必须在身边 grub引导菜单 按任意键进入->e ( ...
- virtualBox虚拟机Ubuntu系统与主机Windows共享文件夹
1.在virtualBox虚拟机中安装Ubuntu系统 2.打开虚拟机后,安装VirtualBox增强功能包(VBoxGuestAdditions),参照下图,如果确认已经安装就直接跳过至第4步. 3 ...
- web前端兼容性问题
传送门:https://www.cnblogs.com/zhoudawei/p/7497544.html