题解 | 链表的奇偶重排
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
struct ListNode* oddEvenList(struct ListNode* head ) {
// write code here
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode* jiHead = head;
struct ListNode* ouHead = head->next;
struct ListNode* p = ouHead;
while(jiHead && jiHead->next && ouHead && ouHead->next){
jiHead->next = ouHead->next;
ouHead->next = jiHead->next->next;
// 向后移动
jiHead = jiHead->next;
ouHead = ouHead->next;
}
// 将奇节点和偶节点连接
jiHead->next = p;
return head;
}
