leetcode 日经贴,Cpp code -Reverse Linked List II
2015-05-05 18:30
676 查看
Reverse Linked List II
/** * 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) { if (!head || !head->next || m >= n) { return head; } ListNode *cur = head, *pre = NULL; int k = n - m; while (--m) { pre = cur; cur = cur->next; } //reverse ListNode *tail = cur; ListNode *h = cur; cur = cur->next; while (cur && k--) { ListNode *next = cur->next; cur->next = h; h = cur; cur = next; } if (pre) { pre->next = h; } else { head = h; } //after reverse tail->next = cur; return head; } };
相关文章推荐
- leetcode 日经贴,Cpp code -Reverse Linked List
- leetcode 日经贴,Cpp code -Linked List Cycle II
- [LeetCode] Reverse Linked List II
- LeetCode-Reverse Linked List II
- leetcode_092 Reverse Linked List II
- 【Leetcode】Reverse Linked List II
- leetcode--Reverse Linked List II
- [leetcode][list][two pointers] 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 92 Reverse Linked List II
- [LeetCode] Reverse Linked List II(!!!)
- 【LeetCode】Reverse Linked List II
- leetcode 92: Reverse Linked List II
- [LeetCode]92. Reverse Linked List II
- leetcode 日经贴,Cpp code -Palindrome Linked List
- leetcode206/92---Reverse Linked List I/II(反转链表)
- leetcode-92. Reverse Linked List II(将单链表部分进行转置)