143. Reorder List LeetCode
2016-03-17 22:26
477 查看
题意:给你一个链表,把链表从 L: L0→L1→…→Ln-1→Ln,变成L0→Ln→L1→Ln-1→L2→Ln-2→…。
题解:把链表的后一般翻转,然后两个链表按规则合并。
题解:把链表的后一般翻转,然后两个链表按规则合并。
class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *pre = NULL; ListNode *cur = head; while(cur != NULL) { ListNode *next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } void reorderList(ListNode* head) { ListNode* fast = head; ListNode* slow = head; if(!fast || !fast->next) return; while(fast) { fast = fast->next; if(fast) fast = fast->next; else break; slow = slow->next; } ListNode* re = slow->next; slow->next = NULL; ListNode* nxt = reverseList(re); ListNode* pre = head; while(pre && nxt) { ListNode* tmp = pre->next; pre->next = nxt; nxt = nxt->next; pre->next->next = tmp; pre = tmp; } } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解