/**
* 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) {
ListNode *newhead=new ListNode(0);
ListNode *odd=head;
ListNode *p=newhead;
if(head==NULL)return NULL;
while(odd->next!=NULL)
{
ListNode *tail=new ListNode(0);
tail->val=odd->next->val;
p->next=tail;
p=p->next;
if(odd->next->next==NULL)break;
odd->next=odd->next->next;
odd=odd->next;
}
odd->next=newhead->next;
return head;
}
};
leetcode 328 Odd Even Linked List
最新推荐文章于 2025-04-21 17:20:40 发布