题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
struct ListNode* oddEvenList(struct ListNode* head ) {
// write code here
if(head==NULL)
return head;
struct ListNode* head1 = NULL;
struct ListNode* ptail1 = NULL;
struct ListNode* head2 = NULL;
struct ListNode* ptail2 = NULL;
struct ListNode* p1 = head, *p2 = head->next;
while (p1) {
if (head1 == NULL)
head1 = ptail1 = p1;
else {
ptail1->next = p1;
ptail1 = p1;
}
if (head2 == NULL)
head2 = ptail2 = p2;
else {
ptail2->next = p2;
ptail2 = p2;
}
if(p2 == NULL)
break;
p1 = p1->next->next;
if (p2->next != NULL)
p2 = p2->next->next;
}
ptail1->next = head2;
return head;
}