博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Reverse Linked List I, II详解
阅读量:4185 次
发布时间:2019-05-26

本文共 1365 字,大约阅读时间需要 4 分钟。


  • 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?
    ************************************************************************/
ListNode* reverseList(ListNode* head) {        if (head==NULL||head->next==NULL)            return head;        ListNode *pre=head;ListNode *cur=pre->next;        pre->next=NULL;        while (cur) {            ListNode *nxt=cur->next;            cur->next=pre;            pre=cur;            cur=nxt;        }        return pre;    }

/************************************************************************

*
* Reverse a linked list from position m to n. Do it in-place and in one-pass.
*
* For example:
* Given 1->2->3->4->5->NULL, m = 2 and n = 4,
*
* return 1->4->3->2->5->NULL.
*
* Note:
* Given m, n satisfy the following condition:
* 1 ≤ m ≤ n ≤ length of list.
*
*
************************************************************************/
这里写图片描述

ListNode* reverseBetween(ListNode* head, int m, int n) {        if (m==n) return head;        ListNode preHead(0),*pre=&preHead;        preHead.next=head;        for (int i=0;i
next; ListNode *cur=pre->next; for (int i=0;i
next; cur->next=nxt->next; nxt->next=pre->next; pre->next=nxt; } return preHead.next; }
你可能感兴趣的文章
M1芯片Mac 安装git
查看>>
M1芯片Mac Homebrew 安装
查看>>
一篇文章看懂ZooKeeper内部原理
查看>>
全面理解Java内存模型
查看>>
Java类型信息详解
查看>>
深入理解Java线程池
查看>>
Java线程堆栈分析
查看>>
Java中子类能否继承父类的私有属性和方法
查看>>
JVM内存模型详解
查看>>
(二)Git--工作区和暂存区、管理修改与撤销
查看>>
(七)Git--自定义Git
查看>>
(五)Git--分支管理
查看>>
(四)Git--远程仓库
查看>>
(六) Git--标签管理
查看>>
java中继承,子类是否继承父类的构造函数
查看>>
什么是Spring Cloud ?
查看>>
Qt下D-Bus的具体运用(软键盘输入法的实现)
查看>>
嵌入式环境的搭建(用于Arm开发板)
查看>>
Qt中文件读取的几种方式
查看>>
pyqt实现界面化编程
查看>>