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 ...
随机推荐
- Linux下用C获取当前时间
Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() { ti ...
- codeforces水题100道 第二十四题 Codeforces Beta Round #85 (Div. 2 Only) A. Petya and Strings (strings)
题目链接:http://www.codeforces.com/problemset/problem/112/A题意:忽略大小写,比较两个字符串字典序大小.C++代码: #include <cst ...
- Java读取maven目录下的*.properties配置文件
public class ReadProperties{ private static String proFileName = "/config/MQSubjectId.propertie ...
- HTML-锚点-JS跳转锚点
window.location.hash使用说明,这篇写的挺详细的 http://www.cnblogs.com/nifengs/p/5104763.html a标签的话是 name,div呢是id, ...
- 【EF框架】使用params参数传值防止SQL注入报错处理
通过SqlParameter传时间参数,代码如下: var param = new List<SqlParameter>(); param.Add(new SqlParameter(&qu ...
- Windows平台下PHP7添加Sqlserver扩展
1.7.0.x 7.0.x的扩展下载地址: Microsoft Drivers for PHP for SQL Server https://www.microsoft.com/en-us/down ...
- Spark学习笔记--Linux安装Spark集群详解
本文主要讲解如何在Linux环境下安装Spark集群,安装之前我们需要Linux已经安装了JDK和Scala,因为Spark集群依赖这些.下面就如何安装Spark进行讲解说明. 一.安装环境 操作系统 ...
- LeetCode 10 Regular Expression Matching(字符串匹配)
题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description '.' Matches any si ...
- [原]git的使用(二)---工作区和暂存区
接着上篇 git的使用(一) http://www.cnblogs.com/horizonli/p/5323363.html 6.工作区和暂存区(中转站) 工作区(Working Directory) ...
- 查看JVM使用的默认的垃圾收集器
一.查看步骤 cmd执行命令: java -XX:+PrintCommandLineFlags -version 输出如下(举例): 针对上述的-XX:UseParallelGC,这边我们引用< ...