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. 怎么在后台修改前台html页面的key、title、description

    public void UpdateMeta(string title, string keyword, string desc) { ; i >= ; i--) { if (this.Head ...

  2. css居中技巧

    1    text-align: center; 只能对图片,按钮,文字等行内元素(display为inline或inline-block等)进行水平居中.在IE6.7中能对任何元素进行水平居中.另外 ...

  3. text-overflow:ellipsis的巧妙运用

    关键字: text-overflow:ellipsis 语法:text-overflow : clip | ellipsis 取值: clip :默认值 .不显示省略标记(...),而是简单的裁切. ...

  4. OpenGL ES 3.0 顶点缓冲区VBO使用

    一般情况下数据都是有CPU从RAM取数据 然后传给GPU去处理,相对于GPU速度要慢一些. 使用VBO技术 可以把数据存储到GPU的内存空间中,这样GPU可以直接从GPU的内存中取得数据进行处理 速度 ...

  5. Linux特殊权限

    ************************     ACL权限       ****************************************** acl权限的出现是为了弥补用户权 ...

  6. js 函数参数形式

    1. var a = function(b,c){ console.log(arguments);}a("1","cc"); ->  ["1&q ...

  7. java记事本

    新知识点 1.撤销 textArea添加一个实现监听接口的类(添加了之后可以一直监视着添加的删除的情况,以便来撤销 textArea.getDocument().addUndoableEditList ...

  8. C++对象数组操作误区

    由于语义上的需要导致语法的上缺陷,所以导致对象数组在C++中存在陷阱. C++语境:一个基类指针或引用是可以指向派生类对象的,以此可来表现C++对运行时多态的需求: 创建一个对象数组将返回首元素的首地 ...

  9. 优化HTTP前端请求构建高性能ASP.NET站点

    HTTP请求的优化  在一个网页的请求过程中,其实整个页面的html结构(就是页面的那些html骨架)请求的时间是很短的,一般是占整个页面的请求时间的10%-20%.在页面加载的其余的时间实际上就是在 ...

  10. (转载)图解Linux系统的系统架构

    我以下图为基础,说明Linux的架构(architecture).(该图参考<Advanced Programming in Unix Environment>) 最内层是硬件,最外层是用 ...