反转一个单链表。
进阶:
链表可以迭代或递归地反转。你能否两个都实现一遍?
详见:https://leetcode.com/problems/reverse-linked-list/description/

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){
return null;
}
ListNode cur=head;
ListNode pre=null;
ListNode next=null;
while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
}

递归实现:

/**
* 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 p=head;
head=reverseList(p.next);
p.next.next=p;
p.next=null;
return head;
}
}

C++实现:

方法一:非递归

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr)
{
return nullptr;
}
ListNode *cur=head;
ListNode *pre=nullptr;
ListNode *next=nullptr;
while(cur)
{
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
};

方法二:递归

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr||head->next==nullptr)
{
return head;
}
ListNode *p=head;
head=reverseList(p->next);
p->next->next=p;
p->next=nullptr;
return head;
}
};

参考:https://www.cnblogs.com/grandyang/p/4478820.html

206 Reverse Linked List 反转链表的更多相关文章

  1. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  2. [leetcode]206. Reverse Linked List反转链表

    Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...

  3. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  4. LeetCode 206. Reverse Linked List倒置链表 C++

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

  5. LeetCode206. Reverse Linked List(反转链表)

    题目链接:https://leetcode.com/problems/reverse-linked-list/ 方法一:迭代反转 https://blog.csdn.net/qq_17550379/a ...

  6. 91. Reverse Linked List 反转链表

    网址:https://leetcode.com/problems/reverse-linked-list/ 直接参考92:https://www.cnblogs.com/tornado549/p/10 ...

  7. 链表 206 Reverse Linked List, 92,86, 328, 2, 445

    表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...

  8. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  9. 206. Reverse Linked List - LeetCode

    Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...

随机推荐

  1. 微信小程序 项目实战(三)list 列表页 及 item 详情页

    1.项目结构 2.list 列表页 (1)数据(逻辑) list.js // pages/list/list.js Page({ /** * 页面的初始数据 */ data: { title: '加载 ...

  2. php验证邮箱

    <?php if(isset($_POST['email'])){ $email = $_POST['email']; if(filter_var($email, FILTER_VALIDATE ...

  3. soapUI系列之—-05 JDBC Request & Xpath Match

    一.配置JDBC Connection String 1. 以Oracle为例,要使用JDBC数据库就要先下一个 oracle JDBC的驱动,下载成功后把它放到soapUI安装目录下的  bin/e ...

  4. 嵌入式开发之davinci---DM8168 8127 8148 HDVPSS中的一些英文缩写解释

    BLEND:Alpha blends input with the graphics.将输入的视频与图形做Alpha融合. CPROC:Color Processing.颜色处理.如动态对比度增强.饱 ...

  5. 2016/3/30 ①投票checkbox ②进度条两个div套起百分比控制内div(width) <div><div></div></div> ③数据库test2 表 diaoyan... 35岁发展方向投票

    分两个页面,要点:提交form 相连action method  两个页面可以合成一个页面action传到自身页面   但分开较清晰 第一个页面vote.php <!DOCTYPE html P ...

  6. vc字符串转换处理:(绝对精华,收集所有的例子)

    vc字符串转换处理:(绝对精华,收集所有的例子) 1.头文件中要定义宏;   #define   UNICODE         #define   _UNICODE     //////////// ...

  7. 设计模式-(9)中介者模式(swift)

    在对象去耦合的模式中,有两种模式:中介者模式,观察者模式 一,概念 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 这个 ...

  8. poyla计数问题

    关于poyla定理,首先推荐两篇很好的文章阅读 2001-----符文杰<poyla原理及其应用> 2008-----陈瑜希<poyla计数法的应用> 在然后就是自己的学习笔记 ...

  9. MultipartResolver实现文件上传功能

    转自:https://www.jb51.net/article/142736.htm springMVC默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文 ...

  10. 微型ORM:PetaPoco 学习资料整理

    github地址:https://github.com/CollaboratingPlatypus/PetaPoco petapoco 实体中字段去掉关联(类似于EF中的NotMap) 微型ORM:P ...