描述

Reverse a singly linked list.

分析

一开始写的时候总感觉没抓到要点,然后想起上数据结构课的教材上有这道题,翻开书一看完就回忆起来了,感觉解法挺巧妙的,不比讨论区的答案差。

这道题是放在链栈后面的,用的也是链栈的思想:

依次将原来栈中的元素出栈,并插入到初值为空的另一个链栈中,这样,当原栈为空时,另一个栈就是原来栈中元素的逆序。

代码如下:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* reverseList(ListNode* head) {
  12. ListNode* p = NULL; //empty "stack"
  13. ListNode* u;
  14. ListNode* l = head; //point to original list
  15. while(l){ //if not NULL
  16. u = l; //u points to l
  17. l = l -> next; //l points to next
  18. u -> next = p; //push the node into "stack" p
  19. p = u; //modify the top pointer
  20. }
  21. head = p; //no effect to answer,but it's a good habbit
  22. return p;
  23. }
  24. };

leetcode解题报告(27):Reverse Linked List的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

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

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

  7. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  8. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  9. LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver

    1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...

随机推荐

  1. condition的使用

    condition 的作用:条件锁 需求: 按需执行三个线程. 用wait,notify的方式: /** * 有序线程 wait,notify版 */ public class OrderThread ...

  2. win server 检查是否是ntfs文件系统

    fsutil fsinfo volumeinfo c: | findstr  /i "文件系统名"

  3. serviceBehaviors_dataContractSerializer_maxItemsInObjectGraph 关键**Behavior

    <behaviors> <serviceBehaviors> <behavior name="STHotel.Product.WCFService.HotelP ...

  4. jvm调优相关

    查找占用cpu过高的线程,并排查原因 1.查找jvm进程 (1)jps -l (2)ps -ef|grep java 这一步骤可以得到进程号,假如进程号为9527 2.查找该jvm进程中占用cpu比较 ...

  5. vue 关于子组件向父组件传值$emit触发无效问题

    先贴上代码 子组件代码 //子组件请求接口,用自己封装的axios getupdate(){ this.$post({ url:this.$apis.unitupdate, postType:'jso ...

  6. SpringBoot+SpringCloud+vue+Element开发项目——搭建开发环境

    1.新建一个项目

  7. 修改ActiveMQ的内存大小

    有时我们需要修改ActiveMQ的内存大小,防止内存溢出! 修改配置文件下-Xmx参数然后重启mq即可: /fs01/apache-activemq-5.15.0/bin/env ACTIVEMQ_O ...

  8. Alluxio : 开源分布式内存文件系统

    Alluxio : 开源分布式内存文件系统 Alluxio is a memory speed virtual distributed storage system.Alluxio是一个开源的基于内存 ...

  9. 《少年先疯队》第八次团队作业:Alpha冲刺第三天

    前言   第三天冲刺会议    时间:2019.6.16   地点:宿舍 3.1 今日完成任务情况以及遇到的问题.   3.1.1今日完成任务情况 姚玉婷:酒店的会员信息的管理,如会员的添加,删除 马 ...

  10. 圆柱模板行业B2B站点打造MIP推送+熊掌号推送+历史普通推送插件

    最近因为做聚合页面http://zhimo.yuanzhumuban.cc/hotkey/list-951.html  内部站点关键词拥有5万的行业词库,所以这么多搜索词库,如何让百度第一时间抓取呢? ...