Leetcode 206. Reverse Linked List
Similar Questions
思路:链表反转。
解法一:迭代。
添加头节点(推荐):不断将当前元素start插入dummy和dummy.next之间,实现反转。
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode reverseList(ListNode head) { 11 if(head == null) return head; 12 ListNode dummy = new ListNode(0); 13 dummy.next = head; 14 ListNode pre = head, start = head.next; 15 while(start != null) { 16 pre.next = start.next; 17 start.next = dummy.next; 18 dummy.next = start; 19 start = pre.next; 20 } 21 return dummy.next; 22 } 23 }
不添加头节点:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode reverseList(ListNode head) { 11 ListNode p0, p1; 12 p0 = null; 13 p1 = head; 14 while(p1 != null) {//确保p1不为空 15 ListNode p2 = p1.next; 16 p1.next = p0; 17 p0 = p1; 18 p1 = p2; 19 } 20 return p0; 21 } 22 }
解法二:递归。
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode reverseList(ListNode head) { 11 if(head == null || head.next == null) return head;//这个判断很重要 12 ListNode p1 = head.next; 13 ListNode p0 = reverseList(p1); 14 p1.next = head; 15 head.next = null; 16 return p0; 17 } 18 }
Next challenges: Reverse Linked List II Binary Tree Upside Down
转载于:https://www.cnblogs.com/Deribs4/p/8406523.html
Leetcode 206. Reverse Linked List相关推荐
- leetCode 206. Reverse Linked List 反转链表
206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /*** ...
- LeetCode 206 Reverse Linked List--反转链表--迭代与递归解法--递归使用一个临时变量,迭代使用3个
此题链接:Reverse Linked List - LeetCode Reverse a singly linked list. Example: Input: 1->2->3-> ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) {i ...
- LeetCode 206 Reverse Linked List 解题报告
题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...
- [swift] LeetCode 206. Reverse Linked List
Reverse a singly linked list. func reverseList(_ head: ListNode?) -> ListNode? {var stack = [Int] ...
- leetcode 206 Reverse Linked List
方法一:头插法 方法二:递归法 #include<iostream> using namespace std; #include<vector> #include<alg ...
- LeetCode Notes_#206 Reverse Linked List(C++,Python)
LeetCode Notes_#206 Reverse Linked List(C++,Python) LeetCode Linked List Contents 题目 思路 思考 解答 C++ P ...
- 【??链表】LeetCode 92. Reverse Linked List II
LeetCode 92. Reverse Linked List II Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4306611.html ...
- [LeetCode] [C++] 206 Reverse Linked List 反转单项链表
题目要求 Reverse a singly linked list. LeetCode 206在线测试 问题描述 给定一个单项链表,将其反转后返回链表头节点. 思路分析1 可以完整的遍历一遍链表,将链 ...
最新文章
- redux 局部刷新_如何使用Redux Observables和刷新令牌API获取新的访问令牌
- LeetCode 1011. 在 D 天内送达包裹的能力(二分查找)
- java面试基础_java面试之基础(总结)
- java连接oracle jdbc连接
- garbor 特征 matlab,Gabor小波滤波用于纹理特征提取
- 电路自学2-储能元件(电容+电感+储能元件的串并联)
- 【视觉-摄像机3】}摄像机镜头--焦距与视角(选相机和镜头)
- 很抱歉,三维地图当前不能在你的国家/地区使用 Excel绘制三维地图问题解决
- jeDate—选择日期后,再点开重新选择时间,日期会被置为今日日期
- [读书笔记]How to Get the Most of Studying
- CAJ文献如何转成PDF?免费全篇转换的方法
- stm32串口自定义协议接收一串十六进制数据(将其中两个字节转化为十进制数据)+部分串口基础知识
- mysql语句大全_MySQL语句大全
- java自动违例设计,java违例
- python 微信群男女比例分析,区域分析,柱状图显示
- 地质调查走上云端 国家地质云平台正式上线背后的技术力量
- oracle数据库的基本教程 pdf,Oracle数据库技术基础教程 PDF 下载
- 电脑galgame全cg存档怎么用_Galgame十二神器:君与彼女与彼女之恋
- W ndows7旗舰版RTM,Windows 7 RTM Build各版ISO详细介绍
- ubuntu linux 下建立 纯L2TP 服务端和客户端