Lintcode35-Reverse Linked List-Easy
35. Reverse Linked List
Reverse a linked list.
Example
Example1:
For linked list 1->2->3, the reversed linked list is 3->2->1
Example2:
For linked list 1->2->3->4, the reversed linked list is 4->3->2->1
Challenge
Reverse it in-place and in one-pass
思路:
递归。
1. 定义一个prev成员变量,并初始化为null, 作为反转后链表的头节点
2. 用临时变量保存剩下的(除头结点的)原链表
3. 使取下来的head结点作为反转后链表的头节点(head.next == prev)
4. prev结点指向反转后链表头节点(prev = head)
5. head重新指向剩余链表的头节点。
6. 重复2-6步骤,知道head指向原链表的null位置。
代码:
public ListNode reverse(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
return prev;
}
Lintcode35-Reverse Linked List-Easy的更多相关文章
- LeetCode--LinkedList--206. Reverse Linked List(Easy)
206. Reverse Linked List(Easy) 题目地址https://leetcode.com/problems/reverse-linked-list/ Reverse a sing ...
- LeetCode_206. Reverse Linked List
206. Reverse Linked List Easy Reverse a singly linked list. Example: Input: 1->2->3->4-> ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
- [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 ...
随机推荐
- 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>
"""给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...
- Linux基础命令---tracepath追踪路由信息
tracepath tracepath指令可以追踪数据到达目标主机的路由信息,同时还能够发现MTU值.它跟踪路径到目的地,沿着这条路径发现MTU.它使用UDP端口或一些随机端口.它类似于Tracero ...
- AURO OtoSys IM100 vs Lonsdor K518ISE: which better?
Comparison: AURO OtoSys IM100 and Lonsdor K518ISE It’s aimed to help make a purchase of decent auto ...
- foreach嵌套遍历循环的问题
在foreach嵌套循环中使用==和equals的问题 JSONArray ja1= new JSONArray(); JSONArray ja2 = new JSONArray(); JSONObj ...
- li设置inline-block后,li左边出现空隙问题。
方法1:在ul设置font-size=0,然后再li再单独设置font-size 方法2:li连着写不要换行,也可以解决. <ul> <li>测试1</li>< ...
- 2019/3/19 wen 运算符
- Centos7.3+uwsgi+Nginx部署Django程序
1. 安装Python,这里我用的是阿里云的centos7.3,自带python2,所以,此步略过,具体安装Python可Google. 2. 安装uwsgi,如果安装失败的话首先,我的Python解 ...
- FFMPEG编译参数解析
Standard options: 基本选项参数 --help 显示此帮助信息|print this message --log[=FILE|yes|no] 记录测试并输出到config.err文件| ...
- go 字符串反转(倒序)
似乎没什么好办法,string的话也得需要先转换成rune再反转再转成string package main import ( "fmt" ) func reverseString ...
- 选择排序法、冒泡排序法、插入排序法、系统提供的底层sort方法排序之毫秒级比较
我的代码: package PlaneGame;/** * 选择排序法.冒泡排序法.插入排序法.系统提供的底层sort方法排序之毫秒级比较 * @author Administrator */impo ...