题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
//双队列,if(i%2)----->容易理解错,奇数成立,偶数不成立 class Solution { public: ListNode* oddEvenList(ListNode* head) { ListNode* nList=new ListNode(0); ListNode* nHead = nList; ListNode* tail=head; int len = 0; queue<ListNode*>sig; queue<ListNode*>dou; while (tail) { tail = tail->next; len++; } tail = head; for ( int i=1;i<=len; i++) { if (i % 2) { dou.push(tail); tail = tail->next; } else { sig.push(tail); tail = tail->next; } } tail = head; while (!dou.empty()) { nList->next = dou.front(); dou.pop(); nList = nList->next; } while (!sig.empty()) { nList->next = sig.front(); sig.pop(); nList = nList->next; } nList->next = nullptr; return nHead->next; } };