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