题目:

Reverse a linked list from position m to n.

Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.

Example

Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.

题解:

Solution 1 ()

class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* first = new ListNode(-);
first->next = head;
ListNode* prev = first; for (int i = ; i < m - ; ++i) {
prev = prev->next;
}
ListNode* cur = prev->next;
for (int i = ; i < n - m; ++i) {
ListNode* tmp = cur->next;
cur->next = tmp->next;
tmp->next = prev->next;
prev->next = tmp;
} return first->next;
}
};

【Lintcode】036.Reverse Linked List II的更多相关文章

  1. 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)

    题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...

  2. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  3. 【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-> ...

  4. 【Lintcode】 035.Reverse Linked List

    题目: Reverse a linked list. Example For linked list 1->2->3, the reversed linked list is 3-> ...

  5. 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

    The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...

  6. 【LeetCode】206. Reverse Linked List (2 solutions)

    Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...

  7. 【一天一道LeetCode】#92. Reverse Linked List II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

  8. 【LeetCode】206. Reverse Linked List

    题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...

  9. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. FZU 2124 FOJ 2124 吃豆人【BFS】

     Problem 2124 吃豆人 Accept: 134    Submit: 575 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  2. jxl切割excel文件

    近期在实施一个项目.当中一项工作是处理历史数据. 客户提供过来的数据是excel表格,超过20万条记录,因为目标系统导入限制,每次仅仅能导入大小不超过8M的文件.所以须要对这些数据进行切割处理.在手工 ...

  3. 在tomcat中用jndi配置数据源启动java web程序

    1.在web.xml中添加: <resource-ref>    <res-ref-name>jdbc/MTSDB</res-ref-name>    <re ...

  4. string去空格

    众所周知,string字符串去除空格的方法有trim()和replace(),区别在于trim()去首尾的空格,但是不能去中间的,而replace可以去除所有的空格. string data1=&qu ...

  5. 深入Asyncio(六)Tasks and Futures

    Tasks and Futures 大多数的工作只涉及到Task.create_task()方法,就像前面代码一样,Future是Task的父类,提供与loop交互的所有功能. Future对象表示某 ...

  6. Java带标签的break 和带标签的continue

    最开始没有学习java 学习的是C语言然后工作开始用java,但当时并没有仔细看过java的书籍,也是大致一翻就看跟C语言很像,了解了基本语法就没有深究了,今天看书开始发现之前没有了解过的语法 带标签 ...

  7. angularcli填坑系列(持续更新...)

    1.在xx.ts中引入css样式无效 @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls ...

  8. django url匹配过程

    ROOT_URLCONF root URLconf module urlpatterns “include” other URLconf modules chops off whatever part ...

  9. python中的raw字符串

    在正则表达式的字符串前面加上r表示这个是一个raw字符串,只以正则表达式的元字符进行解析,不用理会ascii的特殊字符.

  10. Codeforces Round #243 (Div. 1)——Sereja and Two Sequences

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24798219 题目链接 题意:给两个长度分别 ...