LeetCode--LinkedList--206. Reverse Linked List(Easy)
206. Reverse Linked List(Easy)
题目地址https://leetcode.com/problems/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?
solution
题意是给定一个链表,要求将链表反转,且需要分别用迭代和递归实现。
解法一:迭代
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode start = new ListNode(-1);
ListNode p ,temp;
while (head != null)
{
p = head.next;
temp = start.next;
start.next = head;
head.next = temp;
head = p;
}
return start.next;
}
}
解析:构造一个额外的头结点,用链表的头插法即可搞定。
解法二:递归
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) //链表为空或当前结点的下一个结点为空即返回head
return head;
ListNode p = reverseList(head.next); //新链表的头结点
head.next.next = head; //将当前结点的下一个结点的下一个个结点置为head,即反转
head.next = null; //断掉head结点与下一个结点的连接
return p; //返回头结点
}
}
解析:首先判断链表为空或当前结点的下一个结点为空即返回head,否则递归访问链表;当递归条件不满足时,返回新链表的头结点,并将头结点赋值给p,即p成为新链表的头结点。此时,将当前结点的下一个结点的下一个结点置为head,即翻转,然后断掉head结点与下一个结点的连接,最后返回头结点p。
reference
https://leetcode.com/problems/reverse-linked-list/solution/
Notes
1.递归算法的逻辑不太好想清楚,得仔细推敲!
LeetCode--LinkedList--206. Reverse Linked List(Easy)的更多相关文章
- 【LeetCode】206. Reverse Linked List (2 solutions)
Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【一天一道LeetCode】#206. Reverse Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- 【LeetCode】206. Reverse Linked List
题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...
- Leetcode:206. Reverse Linked List
这题直接ac掉 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; while(he ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 206. Reverse Linked List - LeetCode
Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...
- 链表 206 Reverse Linked List, 92,86, 328, 2, 445
表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...
随机推荐
- Centos 开启网络
在VMWare上安装了Centos 7,但是发现网络无法连接. # ping www.baidu.com 确认网络无法连接 # ifconfig 查看centos7中的网络配置属性. 启动网络,开启e ...
- JS 的基础概念
本篇文章主要讲述js的基础知识! 首先,我们要明白什么是JS,JS就是 javascript 的简称,是一种轻量级,弱类型的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能, ...
- javascript: Object对象生成URL参数
code: function makeQuery(queryObject) { const query = Object.entries(queryObject) .reduce((result, e ...
- uniqid用法
uniqid():妙用就是以当前时间微妙为单位,返回的唯一ID 我们可以用到密码加密和接口加密的功能上,比如 $salt = substr(uniqid(rand()), -6);//截取倒数6位$p ...
- Inno Setup: Ask for reboot after uninstall
https://stackoverflow.com/questions/36497580/inno-setup-ask-for-reboot-after-uninstall Use Uninst ...
- Ubuntu 修改 hosts 文件
sudo vi /etc/hosts sudo /etc/init.d/networking restart
- HDU 5725 Game
1. 笔记 题意是求距离的期望(距离仍指连接两点且有效的路径长度的最小值).直观想象可以发现,该距离与曼哈顿距离相比最多多2(可以构造这样的路径). 答案=(任意两点曼哈顿距离的总和 - 至少有一点是 ...
- 交换机上的MAC地址表
拓扑图: 1.首先在R1上的配置: R1(config)#int R1(config)#interface g R1(config)#interface gigabitEthernet 0/0 R1( ...
- 在独立的 Root 和 Home 硬盘驱动器上安装 Ubuntu
安装 Linux 系统时,可以有两种不同的方式.第一种方式是在一个超快的固态硬盘上进行安装,这样可以保证迅速开机和高速访问数据.第二种方式是在一个较慢但很强大的普通硬盘驱动器上安装,这样的硬盘转速快并 ...
- 第七周CorelDRAW课总结
1.这节课学到了什么知识? "交互式透明工具""交互式阴影工具"以及"交互式调和工具"制作水晶表情包. 2.有什么心得体会? 无 3.存在的 ...