Reverse Linked List(反转单向链表)
来源:https://leetcode.com/problems/reverse-linked-list
Reverse a singly linked list.
递归方法:递归调用直到最后一个节点再开始反转,注意保存反转后的头结点返回
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}
Python
# -*- coding:utf-8 -*-
# 递归
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if pHead == None or pHead.next == None:
return pHead
tmp = self.ReverseList(pHead.next)
pHead.next.next = pHead
pHead.next = None
return tmp
迭代方法:两个指针从头开始依次反转,注意保存下一次反转的节点指针
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode pre = head, cur = head.next, next = null;
while(cur != null) {
next = cur.next;
cur.next = pre;
if(pre == head) {
pre.next = null;
}
pre = cur;
cur = next;
}
return pre;
}
}
Python
# -*- coding:utf-8 -*-
# 迭代
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if pHead == None or pHead.next == None:
return pHead
pre_node, cur_node = pHead, pHead.next
while pre_node and cur_node:
tmp_node = cur_node.next
cur_node.next = pre_node
pre_node = cur_node
cur_node = tmp_node
pHead.next = None
return pre_node
Reverse Linked List(反转单向链表)的更多相关文章
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- [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] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [LeetCode] 92. Reverse Linked List II 反向链表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反转链表
Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...
- 206 Reverse Linked List 反转链表
反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...
- LeetCode206. Reverse Linked List(反转链表)
题目链接:https://leetcode.com/problems/reverse-linked-list/ 方法一:迭代反转 https://blog.csdn.net/qq_17550379/a ...
- 91. Reverse Linked List 反转链表
网址:https://leetcode.com/problems/reverse-linked-list/ 直接参考92:https://www.cnblogs.com/tornado549/p/10 ...
随机推荐
- 【hiho1044】状压dp1
题目大意:给定一个长度为 N 的序列,每个位置有一个权值,现选出一些点,满足相邻的 M 个点中至多有 Q 个点被选择,求选出点权的最大值是多少. 题解:若没有相邻的限制,这道题类似于子集和问题,即:背 ...
- flask基础之一
flask基础之一 hello world #从flask这个包中导入Flask这个类 #Flask这个类是项目的核心,以后的很多操作都是基于这个类的对象 #注册url,注册蓝图都是这个类的对象 fr ...
- cron常用表达式
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11905247.html 推荐一个cron表达式生成的网站:https://www.freeformat ...
- node.js 的 中间件 初理解
听说中间件还挺重要,下面梳理一下初认识: 中间件是什么?简单说说http请求服务的过滤,当交给函数处理之前先交给它处理.匹配后会终止,要想再匹配,得加: next. 中间件能解决什么问题?检测用户登录 ...
- 使用HTML和CSS来实现为文字设置图片底纹
先看一下最终实现的效果 图中的hello是文本而不是图片 那么如何实现这种效果呢? HTML部分: 创建一个h1标签 ,标签内容为(hello).通过link标签链接外部样式表style.css. s ...
- mysql WHERE语句 语法
mysql WHERE语句 语法 作用:如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句.珠海大理石平尺 语法:SELECT 列名称 FROM 表名称 WHERE 列 运算 ...
- 放一道比较基础的LCA 的题目把 :CODEVS 2370 小机房的树
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...
- [LightOJ1240]Point Segment Distance 题解
题意简述 原题LightOJ 1240,Point Segment Distance(3D). 求三维空间里线段AB与C. 题解 我们设一个点在线段AB上移动,然后发现这个点与原来的C点的距离呈一个单 ...
- Charles抓取手机https请求
1.下载Charles工具,3.92破解版:http://pan.baidu.com/s/1cko2L4 密码:chmy 2.安装SSL证书,默认安装就可以 3.证书安装成功后,点击详细信息--> ...
- Qt中用QuaZip来压缩和解压缩文件
1.简介 QuaZIP是使用Qt,C++对ZLIB进行简单封装的用于压缩ZIP以及解压缩ZIP的开源库.如果你的Qt项目当中用到了压缩以及解压缩ZIP的话你可以考虑选择使用它. 官方主页:http:/ ...