[Leetcode] Reverse Linked List II
来源:互联网 发布:百鸟朝凤影评知乎 编辑:程序博客网 时间:2024/04/29 17:25
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->NULL
, m = 2 and n = 4,
return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function assert(m<=n); int count = 1; ListNode* vHead = new ListNode(-1); vHead->next = head; ListNode* last = vHead; ListNode* first = head; for(int i=1;i<m;i++) { last = last->next; first = first->next; } ListNode* pre = last->next; ListNode* current = last->next->next; ListNode* temp; for(int i=m;i<n;i++) { temp = current->next; current->next = pre; pre = current; current = temp; } last->next = pre; //Think Through! first->next = current; return vHead->next; }};
- LeetCode[Linked List]: Reverse Linked List II
- LeetCode: Reverse Linked List II
- LeetCode: Reverse Linked List II
- [LeetCode] Reverse Linked List II
- [Leetcode] Reverse Linked List II
- [LeetCode]Reverse Linked List II
- Leetcode: Reverse Linked List II
- [Leetcode]Reverse Linked List II
- [leetcode]Reverse Linked List II
- LeetCode-Reverse Linked List II
- LeetCode Reverse Linked List II
- [leetcode] Reverse Linked List II
- LeetCode - Reverse Linked List II
- [LeetCode] Reverse Linked List II
- LeetCode | Reverse Linked List II
- 【leetcode】Reverse Linked List II
- Leetcode: Reverse Linked List II
- leetcode Reverse Linked List II
- ERROR L107: ADDRESS SPACE OVERFLOW
- 学习笔记_第四章
- 车道检测模块
- Oracle安装错误ora-00922(缺少或无效选项)
- java 模拟get请求
- [Leetcode] Reverse Linked List II
- mahout in action推荐系统阅读笔记(1)
- select()函数以及有关fd_set宏的用法
- jsp页面动态显示服务器时间
- Adapter报错:The content of the adapter has changed
- Drawing a Color Hue Wheel with C#
- 备份,我们对客户最神圣的承诺
- 我是程序员,我为自己代言
- DOS命令备份MSSQL并RAR压缩