LeetCode-206.ReverseLinked 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?
public ListNode reverseList(ListNode head) {//链表 迭代 my
ListNode nhead = null;
ListNode curr = head;
while(null!=curr){
head=head.next;
curr.next=nhead;
nhead=curr;
curr=head;
}
return nhead;
}
递归
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
进阶题
两两交换链表中的结点 LeetCode24 https://www.cnblogs.com/zhacai/p/10559271.html
每 k 个节点一组翻转链表 LeetCode25 https://www.cnblogs.com/zhacai/p/10563446.html
LeetCode-206.ReverseLinked List的更多相关文章
- LeetCode 206. 反转链表(Reverse Linked List) 16
206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...
- 每天一道面试题LeetCode 206 -- 反转链表
LeetCode206 反转链表 思路 代码 # # @lc app=leetcode.cn id=206 lang=python3 # # [206] 反转链表 # # https://leetco ...
- leetcode 206
206. Reverse Linked List Reverse a singly linked list. 翻转一个单链表. 代码如下: /** * Definition for singly-li ...
- LeetCode 206 单链表翻转
https://leetcode.com/problems/reverse-linked-list/ 思路很简单,分别设置三个结点,之后依次调整结点1和结点2的指向关系. Before: pre -& ...
- leetCode:206 反转链表
206. 反转链表 题目:反转一个单链表. 进阶:链表可以迭代或递归地反转.你能否两个都实现一遍? 非递归代码: class Solution { public ListNode reverseLis ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [LeetCode 206] Reverse Linked List 翻转单链表
本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Soluti ...
随机推荐
- ibatis 引入多个model
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "- ...
- URI跳转方式地图导航的代码实践
本文转载至 http://adad184.com/2015/08/11/practice-in-mapview-navigation-with-URI/ 前言 之前介绍了我正在做的是一款定位主打的应用 ...
- hadoop集群操作常用命令
一.HDFS相关 1.启动NameNode sbin/hadoop-daemon.sh start namenode 2.启动DataNode sbin/hadoop-<span style=& ...
- Material Design系列第七篇——Maintaining Compatibility
Maintaining Compatibility This lesson teaches you to Define Alternative Styles Provide Alternative L ...
- eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效
eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效
- Oracle 学习之exists
不相关子查询:子查询的查询条件不依赖于父查询的称为不相关子查询.相关子查询:子查询的查询条件依赖于外层父查询的某个属性值的称为相关子查询,带EXISTS 的子查询就是相关子查询EXISTS表示存在量词 ...
- hdu 4746Mophues[莫比乌斯反演]
Mophues Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327670/327670 K (Java/Others) Total ...
- 在稳定性测试中,将测试结果持续填加进入html报告
公司需要设计一个稳定性测试,就是一直持续的跑不同的用例,直到人为停止,用例基本完成,基本框架思路就是随机选择一个testcase,跑完后输出结果.但存在一个问题,现在的unittest或nose测试报 ...
- Thymeleaf 入门
基本项目结构: Thymeleaf配置: spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false spring.thymeleaf ...
- squid白名单
http_access deny all #取消注释 http_access allow all --> http_access allow xxx_custom_ip #添加系统服务器IP白名 ...