题目

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

分析

反转链表。

一个简单的解法,既然反转该链表,我们把所有节点作为一个输入序列,按照头插法重新构造一个链表即可,它既是所给链表的反转结果。

题目所给提示还有另外两种方法解决,迭代和递归。

没有想到所说的迭代是个什么意思,下面将给出头插法和递归实现的代码!

AC代码

class Solution {
public:
//方法一:头插法
ListNode* reverseList(ListNode* head) {
if (head == NULL)
return head; //反转,即将所有节点按照头插法重新插入一遍即可
ListNode *p = head->next;
head->next = NULL;
while (p)
{
//保存p的后续节点
ListNode *r = p->next;
p->next = head;
head = p;
p = r;
}
return head;
} //方法二:递归实现
ListNode* reverseList2(ListNode* head) {
if (head == NULL)
return head; ListNode *p = head; //反转其余节点组成的链表,将头结点链接到尾部
if (head->next)
{
head = reverseList(head->next);
ListNode *r = head;
while (r->next)
r = r->next;
r->next = p;
}
p->next = NULL;
return head;
}
};

GitHub测试程序源码

LeetCode(206) Reverse Linked List的更多相关文章

  1. LeetCode(92) 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- ...

  2. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

  3. leetcode之旅(9)-Reverse Linked List

    题目描述: Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed ei ...

  4. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...

  5. LeetCode(151) Reverse Words in a String

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  6. LeetCode(25)Reverse Nodes in k-Group

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  7. LeetCode(47)-Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  8. LeetCode(190) Reverse Bits

    题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  9. Leetcode(206)-反转链表

    反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路:反转链表很简 ...

随机推荐

  1. WebApi迁移ASP.NET Core2.0

    WebApi迁移ASP.NET Core2.0 一步一步带你做WebApi迁移ASP.NET Core2.0   随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的AS ...

  2. python实现批量远程执行命令及批量上传下载文件

    #!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...

  3. Redis string(字符串)

    1.getset key newValue   //给key设置value,并返回旧的value,如果没有旧的value,返回nil. 示例: getset age      //age 的值被设置为 ...

  4. 字典(dict),增删改查,嵌套

    一丶字典 dict 用{}来表示  键值对数据  {key:value}  唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 二丶字典的增删改查 1.增 dic[k ...

  5. HTML5标签选择指引

  6. 常用的图片相关方法,读取,保存,压缩,缩放,旋转,drawable转化

    import android.content.Context; import android.content.res.AssetManager; import android.content.res. ...

  7. Java中方法的继承以及父类未被子类覆盖的方法调用的问题

    在看java继承这一块的时候发现了一个问题,即父类未被子类覆盖的方法是如何调用的? 是子类拥有了父类的该方法只是没有显示表示,还是子类调用了父类的该方法. 为此做了一下验证 代码如下: public ...

  8. External Pricing in C4C and ERP

    从下图可以看出,C4C的Opportunity,Sales Quote和Sales Order这些business transaction没有自己的pricing engine,使用的是在ERP Pr ...

  9. 2407: C语言习题 整数转换成字符串

    2407: C语言习题 整数转换成字符串 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 917  Solved: 416[Submit][Status] ...

  10. 2018.4.18 Ubuntu 的telnet命令详解

    Ubuntu 的telnet命令详解 1.作用用途 Telnet 命令通常用来远程登录,Telnet 程序是基于 Telnet 协议的远程登录客户端程序.Telnet 协议是TCP/IP协议族中的一员 ...