/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ void Separate(struct ListNode* head)//将链表奇偶节点重排 { struct ListNode* phead = head->next; //偶节点的头 struct ListNode* tmp = phead->next; //指向原链表head的指针 struct ListNode* ptmp = phead; //指向tmp的前一个节点的指针 struct ListNode* pnode = phead; //新链表的尾指针 head->next = phead->next; for (int i = 3; tmp != NULL; tmp = tmp->next, i++) { if (i % 2 == 0) { ptmp->next = tmp->next; pnode->next = tmp; pnode = tmp; } else { ptmp = tmp; } } ptmp->next = phead; pnode->next = NULL; } struct ListNode* oddEvenList(struct ListNode* head ) { // write code here if (head == NULL || head->next == NULL || head->next->next == NULL) //节点数小于3就直接返回 { return head; } Separate(head); return head; }