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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

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

题目大意就是给一个单链表,给定一个起点和一个终点,反转起点和终点之间的链表,要求:原地反转,一次遍历。

解题思路:因为已经限制了1 ≤ m ≤ n ≤ length of list,设定头节点指向第一个元素,工作指针先走m步,采用头插法来重新构建m到n之间的数据,最后把m位置上的next指针指向原来的n+1的位置。

这次代码写的有点纠结,Java操作这些不如c++熟练。

public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || m == n) {
return head;
}
int count = n - m;
ListNode p = new ListNode(0);
ListNode q;
ListNode headp = new ListNode(0);
ListNode res = headp;
p.next = head;
headp.next = head;
while (--m > 0) {
headp = headp.next;
}
p = headp.next;
q = p.next;
ListNode last = p;
headp.next = null;
while (count-- >= 0) {
p.next = headp.next;
headp.next = p;
if (q != null) {
p = q;
q = q.next;
} else {
p = null;
}
}
last.next = p;
/*while(res!=null){
System.out.println(res.val);
res=res.next;
}*/
return res.next;
}

Reverse Linked List II——LeetCode的更多相关文章

  1. Reverse Linked List II [LeetCode]

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  2. Reverse Linked List II leetcode java

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1 ...

  3. lc面试准备:Reverse Linked List II

    1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...

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

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

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

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

  6. 【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-pass. F ...

  7. 【原创】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 ...

  8. 【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-pass. F ...

  9. leetcode -day30 Reverse Linked List II

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

随机推荐

  1. Meth | elementary OS常用配置

    一,搜狗输入法 sudo apt-get remove ibussudo add-apt-repository ppa:fcitx-team/nightlysudo apt-get updatesud ...

  2. HDU 4462(暴力枚举)

    因为题目当中的k比较小k <= 10,所以可以直接枚举,题目里面由两个trick, 一个是如果每个点都可以放稻草人的话,那么答案是0, 另外一个就是如果可以放稻草人的点不用被照到.知道了这两个基 ...

  3. Android 自定义日历

    好久没来写博客了,这半年多发生了好多的事情,废话不多说,今天在公司里比较闲在,写一篇最近写的公司用到的控件——日历控件. 控件的功能比较少,根据需求只有选择开始时间和结束时间并返回时间段. 效果图如下 ...

  4. dom4j 笔记【转】

    SAXReader reader = new SAXReader(); Document doc = reader.read(...); List childNodes = doc.selectNod ...

  5. 配置中的address不能重复

    <jaxws:endpoint  implementor="com.service.imp.UserServiceImpl" address="/user" ...

  6. Swift - 23 - 选择结构

    //: Playground - noun: a place where people can play import UIKit var rating = "A" // if - ...

  7. 使用less函数实现不同背景的CSS样式

    今天在公司遇到一个比较特殊的需求,需要完成这样的布局,如下图: 每一个块的背景需要不同,而其他都是相同的,这时候就应该把背景提出来单独写成一个CSS样式类. 那么问题来了,有四个不同的背景需要写4个基 ...

  8. angular.js学习手册(二)

    如何使用angularjs? 各个 angular.js 版本下载: https://github.com/angular/angular.js/releases 下载完之后,在你需要使用angula ...

  9. Hibernate中分页

    query.setFirstResult(4);query.setMaxResults(5);       这两个方法就是hibernate的分页

  10. canvas 渐变

    那么第一种渐变方式就是LinearGradient,具体实施就是以下代码: var colorStyle=context.createLinearGradient(0,0,0,HEIGHT); col ...