Similar Questions

Reverse Linked List II Binary Tree Upside Down Palindrome Linked List

思路:链表反转。

解法一:迭代。

添加头节点(推荐):不断将当前元素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相关推荐

  1. leetCode 206. Reverse Linked List 反转链表

    206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /*** ...

  2. LeetCode 206 Reverse Linked List--反转链表--迭代与递归解法--递归使用一个临时变量,迭代使用3个

    此题链接:Reverse Linked List - LeetCode Reverse a singly linked list. Example: Input: 1->2->3-> ...

  3. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) {i ...

  4. LeetCode 206 Reverse Linked List 解题报告

    题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...

  5. [swift] LeetCode 206. Reverse Linked List

    Reverse a singly linked list. func reverseList(_ head: ListNode?) -> ListNode? {var stack = [Int] ...

  6. leetcode 206 Reverse Linked List

    方法一:头插法 方法二:递归法 #include<iostream> using namespace std; #include<vector> #include<alg ...

  7. LeetCode Notes_#206 Reverse Linked List(C++,Python)

    LeetCode Notes_#206 Reverse Linked List(C++,Python) LeetCode Linked List  Contents 题目 思路 思考 解答 C++ P ...

  8. 【??链表】LeetCode 92. Reverse Linked List II

    LeetCode 92. Reverse Linked List II Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4306611.html ...

  9. [LeetCode] [C++] 206 Reverse Linked List 反转单项链表

    题目要求 Reverse a singly linked list. LeetCode 206在线测试 问题描述 给定一个单项链表,将其反转后返回链表头节点. 思路分析1 可以完整的遍历一遍链表,将链 ...

最新文章

  1. redux 局部刷新_如何使用Redux Observables和刷新令牌API获取新的访问令牌
  2. LeetCode 1011. 在 D 天内送达包裹的能力(二分查找)
  3. java面试基础_java面试之基础(总结)
  4. java连接oracle jdbc连接
  5. garbor 特征 matlab,Gabor小波滤波用于纹理特征提取
  6. 电路自学2-储能元件(电容+电感+储能元件的串并联)
  7. 【视觉-摄像机3】}摄像机镜头--焦距与视角(选相机和镜头)
  8. 很抱歉,三维地图当前不能在你的国家/地区使用 Excel绘制三维地图问题解决
  9. jeDate—选择日期后,再点开重新选择时间,日期会被置为今日日期
  10. [读书笔记]How to Get the Most of Studying
  11. CAJ文献如何转成PDF?免费全篇转换的方法
  12. stm32串口自定义协议接收一串十六进制数据(将其中两个字节转化为十进制数据)+部分串口基础知识
  13. mysql语句大全_MySQL语句大全
  14. java自动违例设计,java违例
  15. python 微信群男女比例分析,区域分析,柱状图显示
  16. 地质调查走上云端 国家地质云平台正式上线背后的技术力量
  17. oracle数据库的基本教程 pdf,Oracle数据库技术基础教程 PDF 下载
  18. 电脑galgame全cg存档怎么用_Galgame十二神器:君与彼女与彼女之恋
  19. W ndows7旗舰版RTM,Windows 7 RTM Build各版ISO详细介绍
  20. ubuntu linux 下建立 纯L2TP 服务端和客户端

热门文章

  1. 实战|手把手教你训练一个基于Keras的多标签图像分类器
  2. CodeForces 931C Laboratory Work 水题,构造
  3. 编译原理预测分析程序
  4. eclipse链接mycat报数据源初始化失败
  5. Python高级用法总结
  6. PHP中的__get()和__set()方法获取设置私有属性
  7. js Date对象总结
  8. SharePoint 跨域还原网站一则
  9. 关于 ASP.NET MVC 中的视图生成
  10. Discuz! 防御CC攻击的设置办法