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 ...
随机推荐
- pandas 的index用途
# pandas的索引index的用途 # 把数据存储于普通的column列也能用于数据查询,那使用index有什么好处? # 1.更方便的数据查询 # 2.使用index可以获得性能提升 # 3. ...
- bzoj4817 & loj2001 [Sdoi2017]树点涂色 LCT + 线段树
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4817 https://loj.ac/problem/2001 题解 可以发现这个题就是 bzo ...
- 初学oracle第三天
体系结构 Oracle 采取的是 Client/Server 架构. 3.1 Client 3.1.1 Sqlplus 这是一个轻量级的功能强大的客户端, 是 dba 必须掌握的工具. 我们可以配 ...
- 【Heaven Cow与God Bull】题解
题目 Description __int64 ago,there's a heaven cow called sjy... A god bull named wzc fell in love with ...
- React Native 之项目的启动
运行项目有两种方法 1. 到根目录,执行 react-native run-ios 命令 会开启一个本地服务,加载jsbundle文件,然后是去index.js文件 import {AppRegist ...
- 小波神经网络(WNN)
人工神经网络(ANN) 是对人脑若干基本特性通过数学方法进行的抽象和模拟,是一种模仿人脑结构及其功能的非线性信息处理系统. 具有较强的非线性逼近功能和自学习.自适应.并行处理的特点,具有良好的容错能力 ...
- 把图片画到画布上,适应PC和移动端
画一张图片到画布上 <canvas id="myCanvas" width="1000px" height="200px" >您 ...
- Anaconda-navigator 打不开的解决方法(亲测有效!)
本文链接:https://blog.csdn.net/qq_42489092/article/details/92208822method_1:每当你打不开应用时,不妨试一下:用管理员身份运行 请用管 ...
- Spring Cloud教程(十)自定义引导配置属性源
可以通过在org.springframework.cloud.bootstrap.BootstrapConfiguration键下添加条目/META-INF/spring.factories来训练引导 ...
- h5新标签介绍
html5新增了一些标签 这些标签都是语义标签,可以帮助我们更好的理解,代码中的意思:(都是双标签) 案例: 将语义标签翻译过来为 <div class="header"&g ...