-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
43 lines (39 loc) · 1.08 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head)
{
ListNode *oddList = new ListNode(0);
ListNode *evenList = new ListNode(0);
ListNode *oddIte = oddList;
ListNode *evenIte = evenList;
for(int i=1;head != NULL;i++)
{
if(i & 0x01) // 计数,判断奇偶
{
oddIte->next = head;
oddIte = oddIte->next;
head = head->next;
oddIte->next = NULL;
}
else
{
evenIte->next = head;
evenIte = evenIte->next;
head = head->next;
evenIte->next = NULL;
}
}
oddIte->next = evenList->next; //偶数链接到奇数链后
head = oddList->next;
delete oddList, evenList; // 回收两个头节点
return oddList->next;
}
};