## 一.题目描述
![](https://box.kancloud.cn/2016-01-05_568bb5ee20b58.jpg)
## 二.题目分析
直接按照题目的要求求解,设ListNode *head为待处理的链表,算法包括以下步骤:
1\. 将链表head分为前后两部分,前半部分链表head1 和后半部分链表head2;
2\. 将后半段链表head12做逆序操作;
3\. 合并head1, head2;
## 三.示例代码
~~~
struct ListNode
{
int value;
ListNode *next;
ListNode(int x) : value(x), next(NULL){};
};
class Solution
{
public:
ListNode * reorderList(ListNode *head) {
if (head == NULL || head->next == NULL)
return head;
ListNode *slow = head;
ListNode *fast = head;
ListNode *cut = head;
while (fast && fast->next)
{
cut = slow;
slow = slow->next;
fast = fast->next->next;
}
cut->next = NULL;
slow = reverseList(slow);
ListNode *result = mergeList(head, slow);
return result;
}
ListNode* reverseList(ListNode *head) // 翻转链表
{
if (head == NULL || head->next == NULL)
return head;
ListNode *prev = head;
ListNode *curr = head->next;
ListNode *temp = curr;
prev->next = NULL;
while (curr)
{
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
return prev;
}
ListNode* mergeList(ListNode *head1, ListNode *head2)
{
ListNode* temp1 = head1;
ListNode* temp2 = head2;
ListNode* pMerge = head1;
bool flag = true;
while (temp1 != NULL && temp2 != NULL)
{
if (flag && temp2 != NULL)
{
temp1 = head1->next;
head1->next = temp2;
head1 = head1->next;
flag = false;
}
if (!flag && temp1 != NULL)
{
temp2 = head1->next;
head1->next = temp1;
head1 = head1->next;
flag = true;
}
}
return pMerge;
}
};
~~~
测试结果:
![](https://box.kancloud.cn/2016-01-05_568bb5ee3a2af.jpg)
**四.总结**
这道题需要对链表进行翻转操作&对两个链表进行合并操作,实现本身不难,但代码只是随便一写,比较繁琐,需要后期再优化。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching