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. javascript touch事件

    touchstart : 當手指觸摸屏幕時觸發:即使已經有一個手指放在了屏幕上也會觸發. touchmove : 當手指在屏幕上滑動時連續的觸發,在這個事件發生期間,商用preventDefault( ...

  2. Android 中Webview 自适应屏幕

    随笔 - 478  文章 - 3  评论 - 113 Android 中Webview 自适应屏幕   webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomCon ...

  3. js异步的理解---千呼万唤始出来啊!

    编译完成后(先分配给变量空间和function(){}命名的函数,var = function(){}这种函数也仅仅只是分配了个空间,还没有赋值个函数给他!),调用了若不是undefined就执行, ...

  4. Linq101-Partitioning

    using System; using System.Linq; namespace Linq101 { class Partitioning { /// <summary> /// Th ...

  5. JavaScript Unicode字符操作

    charCodeAt() 方法 定义和用法charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数.方法 charCodeAt() 与 ...

  6. (转)如何将ecshop首页主广告位的flash轮播替换为js轮播

    转之--http://www.ecshoptemplate.com/article-1710.html 这个ecshop很常见,因为现在比起flash难以修改,js更加符合人们的使用习惯,而默认ecs ...

  7. Linux命令行下svn ignore忽略文件或文件夹用法

    一.忽略单个目录 1.忽略文件夹 假如目录oa.youxi.com是从svn checkout出来的,在服务器本地目录添加了material,但是不希望把material加入版本控制,因此我们需要忽略 ...

  8. Scene is unreachable due to lack of entry points and does not have an identifier for runtime access

    使用Storyboard时出现以下警告: warning: Unsupported Configuration: Scene is unreachable due to lack of entry p ...

  9. Hibernate 一对多单向关联Demo

    以Classes[班级]和Student[学生]为例的Demo Classes .java public class Classes implements Serializable { private ...

  10. 使用Maven构建javaWeb项目时,启动tomcat出错:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.conte

    在初学使用maven构建javaWeb的项目的时候,启动tomcat加载时,总是提示如下错误,辛苦一番终于找到解决办法. 严重: Error configuring application liste ...