2018-09-11 22:58:29

一、Reverse Linked List

问题描述:

问题求解:

解法一:Iteratively,不断执行插入操作。

    public ListNode reverseList(ListNode head) {
if (head == null) return null;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = head;
ListNode then = null;
while (cur.next != null) {
then = cur.next;
cur.next = then.next;
then.next = dummy.next;
dummy.next = then;
}
return dummy.next;
}

解法二:Recursively,不断向newHead前面加入新的节点

    public ListNode reverseList(ListNode head) {
/* recursive solution */
return reverseListInt(head, null);
} private ListNode reverseListInt(ListNode head, ListNode newHead) {
if (head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}

二、Reverse Linked List II

问题描述:

问题求解:

反转链表一直是一个很经典的问题,本题中其实是最经典的全局反转的一个改进和加深,本题的求解思路完全可以套用到全局反转中。

本题实际使用的思路是一种插入的思路,维护了三个指针prev,cur,then。

prev : 初始位置的前一个位置,始终不变,后续就是在prev后进行插入;

cur : 不断迭代,指向需要插入的节点的前一个位置;

then : cur的下一个节点,是每次需要进行插入的节点,同时需要不断迭代。

    public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prev = dummy;
ListNode cur = dummy;
ListNode then = null;
for (int i = 0; i < m; i++) {
prev = cur;
cur = cur.next;
then = cur.next;
}
for (int i = 0; i < n - m; i++) {
cur.next = then.next;
then.next = prev.next;
prev.next = then;
then = cur.next;
}
return dummy.next;
}

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

  1. [Swift]LeetCode206. 反转链表 | Reverse Linked List

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

  2. LeetCode 206:反转链表 Reverse Linked List

    反转一个单链表. Reverse a singly linked list. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3- ...

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

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

  4. leetcode 206 反转链表 Reverse Linked List

    C++解法一:迭代法,使用前驱指针pre,当前指针cur,临时后继指针nxt: /** * Definition for singly-linked list. * struct ListNode { ...

  5. 翻转链表reverse linked list:全部,m~n

    全部 [抄题]: Reverse a singly linked list. [思维问题]: 以为要用dummy node [一句话思路]: 直接全部转过来就行了,用dummy node反而多余 [输 ...

  6. 链表-Reverse Linked List II

    [题目要求直接翻转链表,而非申请新的空间] 这道题的一个关键在于,当m=1时,需要翻转的链表段前没有其他的结点(leetcode的测试用例不含头结点),这个特例给解题带来了一点小小的困难.一个比较直观 ...

  7. 链表-Reverse Linked List

    /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...

  8. (链表)反转链表Reverse List

    逆转链表是简单而又简单的链表问题,其问题的方法之一可以设置三个指针,一个指向当前结点,一个指向前驱结点,一个指向后继指针 代码如下: class Solution { public: ListNode ...

  9. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

随机推荐

  1. 叶亚明:合格CTO的六要素(转)

    叶亚明,携程旅行网CTO & 高级技术副总裁,负责携程的移动.Online.呼叫中心等的技术架构.开发及运营.在加入携程之前,叶亚明是ebay.com技术平台总监,领导ebay.com几代网站 ...

  2. windows下rabbitmq-c编译(带openssl、无需MinGW)

    因为项目原因,需要使用到rabbitmq的c客户端库.首先,参见上一篇windows下openssl编译,如果已经使用cmake编译过了,则先delete cache(File-Delete Cach ...

  3. py4CV例子3Mnist识别和ANN

    1.什么是mnist数据集:  , , ], ,,,,,,,,]], ., ., , , ], ,,,,,,,,]], ., ., , , ])) animals_net.setTermCriteri ...

  4. 20145204张亚军——web安全基础实践

    web安全基础实践 实验后回答问题 1.SQL注入原理,如何防御 SQL注入:就是通过把SQL命令插入到"Web表单递交"或"输入域名"或"页面请求& ...

  5. Bugku-CTF之web2-听说聪明的人都能找到答案

    Day1   听说聪明的人都能找到答案   http://123.206.87.240:8002/yanzhengma/  

  6. 使用TortoiseGit从GitHub下拉上传代码配置

    转载:http://baijiahao.baidu.com/s?id=1579466751803515477&wfr=spider&for=pc 转载:https://blog.csd ...

  7. ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 45, found 42. Created with MySQL 50560, now running 50725. Please use mysql_upgrade to fix this error.

    centos7.5 登入mysql,进行授权报错 问题: mysql> grant all privileges on *.* to 'lvhanzhi'@'%' identified by ' ...

  8. (转)Spring Cloud(一)

    (二期)22.微服务框架spring cloud(一) [课程22]spirng c...简介.xmind54KB [课程22]spirng cl...架构.xmind0.5MB [课程22]负载均. ...

  9. P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

    吐槽 数据太水了吧,我AC自动机的trie建错了结果只是RE了两个点,还以为数组开小了改了好久 思路 看到多模板串,字符串匹配,且模板串总长度不长,就想到AC自动机 然后用栈维护当前的字符串位置,如果 ...

  10. 《操作系统_时间片轮转RR进程调度算法》

    转自:https://blog.csdn.net/houchaoqun_xmu/article/details/55540250 时间片轮转RR进程调度算法 一.概念介绍和案例解析时间片轮转法 - 基 ...