思路并没有用到链表的知识,运用到的时vector容器,哈哈哈,这个还是太好用。代码还算简单
/**
* 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) {
// write code here
vector<int>vec;
ListNode*tmp = head;
int n =0;
while(tmp!=NULL)
{
vec.push_back(tmp->val);
tmp =tmp->next;
n++;
}
tmp = head;
if(n%2)
{
for(int i =0;i<=n-1;i+=2)
{
tmp->val = vec[i];
tmp=tmp->next;
}
for(int i =1;i<=n-2;i+=2)
{
tmp->val = vec[i];
tmp= tmp->next;
}
}
else{
for(int i =0;i<=n-2;i+=2)
{
tmp->val = vec[i];
tmp=tmp->next;
}
for(int i =1;i<=n-1;i+=2)
{
tmp->val = vec[i];
tmp= tmp->next;
}
}
return head;
}
};