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.

Summary: First writes down the reverse parts (m to n) , then handles nodes before the m-th node and after the n-th node.

 class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode * current = head;
ListNode * pre_m_node = NULL;
ListNode * new_head = NULL;
int i = ;
while(current != NULL) {
if(i == m -)
break;
pre_m_node = current;
i ++;
current = current -> next;
}
//reverse m to n
ListNode * pre_n_node = current;
int num_of_reverse_op = ;
int num_of_nodes_to_reverse = n - m + ;
while(num_of_reverse_op < num_of_nodes_to_reverse) {
ListNode * pre_head = new_head;
new_head = current;
current = current -> next;
num_of_reverse_op ++;
new_head -> next = pre_head;
}
//connect rest node after the nth node
pre_n_node -> next = current; if(pre_m_node != NULL) {
pre_m_node -> next = new_head;
return head;
} else {
return new_head;
}
}
};

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 Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用?

    X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的.通过在meta中设置X-UA-Compatible的值,可以指定网页的兼容性模式设置. 在网页中指定的模式优 ...

  2. Populating Display Item Value On Query In Oracle Forms

    Write Post-Query trigger for the block you want to fetch the field value for display item.ExampleBeg ...

  3. celery入门

    认识 这里有几个概念,task.worker.broker.顾名思义,task 就是老板交给你的各种任务,worker 就是你手下干活的人员. 那什么是 Broker 呢? 老板给你下发任务时,你需要 ...

  4. How to evaluate a transimpedance amplifier (part 2)

    In my previous blog on "How to evaluate a transimpedance amplifier, part 1", we looked at ...

  5. Metasploit基础命令

    msf > show exploits 列Metasploip的所有可用的渗透测试框架.在MSF终端中可以针对渗透测试中发现的安全漏洞来实施相应的渗透攻击. msf > show auxi ...

  6. 5.Primitive, Reference, and Value Types

    1.Programming Language Primitive Types primitive types:Any data types the compiler directly supports ...

  7. FJNU 1154 Fat Brother And His Love(胖哥与女神)

    FJNU 1154 Fat Brother And His Love(胖哥与女神) Time Limit: 2000MS   Memory Limit: 257792K [Description] [ ...

  8. Linux命令工具基础02 文件及目录管理

    文件及目录管理 文件管理不外乎文件或目录的创建.删除.查询.移动,有mkdir/rm/mv 文件查询是重点,用find来进行查询:find的参数丰富,也非常强大: 查看文件内容是个大的话题,文本的处理 ...

  9. 【CC评网】2013.第44周 把握每天的第一个小时

    [CC评网]2013.第44周 把握每天的第一个小时 更简单的格式 终于投入到markdown的怀抱.让博客的写作回归到内容本身,同时也能保证阅读的良好体验:如果有心情,写个js,提取h3 h2标题组 ...

  10. 高效使用Vector

    参考网页: http://www.cnblogs.com/biyeymyhjob/archive/2013/05/11/3072893.html#undefined 1.初始化的时候,最好先用rese ...