题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
struct ListNode* oddEvenList(struct ListNode* head ) {
struct ListNode odd = {0};
struct ListNode even = {0};
struct ListNode* p_odd = &odd;
struct ListNode* p_even = &even;
struct ListNode* cur = head;
int count = 1;
if (head == NULL || head->next == NULL) {
return head;
}
while (cur) {
if (count % 2) {
p_odd->next = cur;
p_odd = p_odd->next;
} else {
p_even->next = cur;
p_even = p_even->next;
}
cur = cur->next;
count++;
}
p_even->next = NULL;
p_odd->next = even.next;
return odd.next;
}
struct ListNode odd = {0};
struct ListNode even = {0};
struct ListNode* p_odd = &odd;
struct ListNode* p_even = &even;
struct ListNode* cur = head;
int count = 1;
if (head == NULL || head->next == NULL) {
return head;
}
while (cur) {
if (count % 2) {
p_odd->next = cur;
p_odd = p_odd->next;
} else {
p_even->next = cur;
p_even = p_even->next;
}
cur = cur->next;
count++;
}
p_even->next = NULL;
p_odd->next = even.next;
return odd.next;
}