思路如图:必须奇偶结点交替作业
这道题找到while的条件是关键!
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (head == NULL || head->next == NULL)return head;
//双指针
ListNode*odd_head = head;//奇链表头结点
ListNode*odd_t = head;
ListNode*even_head = head->next;//偶链表头结点
ListNode*even_t = head->next;
while (even_t!=NULL&&even_t->next != NULL)//奇偶交替作业
{
odd_t->next = even_t->next;
odd_t = odd_t->next;
even_t->next = odd_t->next;
even_t = even_t->next;
}
odd_t->next = even_head;
return head;
}
};