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?

|prev  |cur  |next
 v      v     v
       ---   ---   ---         ---
NULL   | |-->| |-->| |-->...-->| |-->NULL
       ---   ---   ---         ---

                    I

              3     5
       2|prev |cur  |next
        v     v     v
       ---   ---   ---         ---
NULL<--| |<--| |   | |-->...-->| |-->NULL

     1 --- 4 ---   ---         ---

主要是头结点平移时候注意cur->next是怎么连接的。

pre->next=head->next;而不是cur->next;

/*** 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) return head;ListNode* temp=new ListNode(NULL);temp->next=head;ListNode* cur=head;while(cur->next){ListNode* pre=cur->next;cur->next=pre->next;pre->next=temp->next;//最主要的一步  怎么倒置连接temp->next=pre;}return temp->next;}
};

递归解法的思路是,不断的进入递归函数,直到head指向最后一个节点,p指向之前一个节点,然后调换head和p的位置,再返回上一层递归函数,再交换p和head的位置,每次交换后,head节点后面都是交换好的顺序,直到p为首节点,然后再交换,首节点就成了为节点,此时整个链表也完成了翻转

/*** 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 || !head->next) return head;ListNode *p = head;head = reverseList(p->next);p->next->next = p;p->next = NULL;return head;}
};
/*** 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==NULL || head->next==NULL) return head;ListNode* p=head;ListNode* cur=head->next;p->next=NULL;while(cur!=NULL){ListNode* temp=cur->next;cur->next=p;p=cur;cur=temp;}return p;}
};


Reverse Linked List相关推荐

  1. LeetCode 92. Reverse Linked List II--Python 解法--反转部分链表--笔试算法题

    题目地址:Reverse Linked List II - LeetCode Reverse a linked list from position m to n. Do it in one-pass ...

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

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

  3. LeetCode 206. 反转链表(Reverse Linked List) 16

    206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...

  4. LeetCode 92. Reverse Linked List II

    92. Reverse Linked List II Reverse a linked list from position m to n. Do it in one-pass. 将位置m的链接列表反 ...

  5. Leetcode 206. Reverse Linked List

    Similar Questions Reverse Linked List II Binary Tree Upside Down Palindrome Linked List 思路:链表反转. 解法一 ...

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

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

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

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

  8. 反转链表 Reverse Linked List

    2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...

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

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

  10. Leetcode日练笔记31 #19 #206 # 203 Rmv Nth Node From End Reverse Linked List Rmv Linked List Elements

    Given the head of a linked list, remove the nth node from the end of the list and return its head. E ...

最新文章

  1. Oracle安装错误“程序异常终止
  2. ROS学习:创建机器人的urdf
  3. python操作dom_Python Dom 的介绍和使用day1
  4. 重装mysql遇到的问题
  5. ArcGIS实验教程——实验十三:栅格空间插值分析
  6. 华为小米入场,能拯救乐视互联网电视挖的坑吗?
  7. matlab 状态空间的波特图,MATLAB:对于状态空间方程的系统辨识
  8. 浙江商人立下的22条规矩
  9. EXCEL 在空值中批量填入某个数值
  10. pcr扩增的原理和步骤
  11. hdu5336XYZ and Drops
  12. Maven历史版本下载
  13. 读《麦田里的守望者》(塞林格)
  14. vscode遇到无法访问此网站问题的两种解决方法
  15. python数据处理7: matplotlib绘图保存图片深入
  16. 淋巴细胞转化中PHP的作用,淋巴细胞转化试验范围|意义
  17. 如何从Lytro 相机中获取图像阵列
  18. 起点小说免费看 Scrapy爬取起点小说网数据导入MongoDB数据
  19. 软件缺陷分析——软件测试之犯罪心理学
  20. redis queue_在Redis上通过Easy Message Queue扩展微服务

热门文章

  1. android 分区layout以及虚拟内存布局-小结
  2. 技术团队新官上任之中层篇
  3. java速查表_Java8新功能速查表 - 穿梭于偶然
  4. 多媒体制作技术心得体会_论多媒体教学的改革与创新
  5. 在电脑桌面怎样用命令开启远程桌面?
  6. asp.net core mvc权限控制:分配权限
  7. IE 市场份额暴跌,Edge 能否守住微软的辉煌
  8. c++之五谷杂粮---2
  9. win7系统自带截图工具快捷键是什么?怎么设置快捷键
  10. key可以重复的map集合:IdentityHashMap