题意
将链表按照奇偶分开(节点编号而不是值)
思路
模拟一下就好,链表题注意边界
代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (!head) return NULL;
ListNode* head1 = head, *head2 = head->next;
ListNode* p1 = head1, *p2 = head2;
while (p1 && p2 && p1->next && p2->next) {
p1->next = p1->next->next;
p2->next = p2->next->next;
p1 = p1->next;
p2 = p2->next;
}
p1->next = head2;
return head1;
}
};