题解 | #链表的奇偶重排#
链表的奇偶重排
http://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ ListNode* oddEvenList(ListNode* head) { if (!head || !head->next) return head; ListNode* odd = head; ListNode* even = head->next; ListNode* temp = even; while (odd->next && odd->next->next) { // 类似于删除链表节点的操作,把偶数节点删掉 // 对应上图,就相当于1号点直接指向了3号点 odd->next = odd->next->next; // 同理,2号点指向了4号点 even->next = even->next->next; // 指向下一个节点,就是删除前的下2个节点 // 对应上图,就是从1号点跳到3号点 odd = odd->next; even = even->next; } odd->next = temp; return head; } };https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3?tpId=295&tqId=1073463&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj