给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。
不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。
给定一个单链表L: L0→L1→…→Ln-1→Ln,
重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→…
必须在不改变节点值的情况下进行原地操作。
样例:
给出链表 1->2->3->4->null,重新排列后为1->4->2->3->null。
#ifndef C99_H
#define C99_H
#include<iostream>
#include<vector>
using namespace std;
class ListNode{
public:
int val;
ListNode *next;
ListNode(int val){
this->val = val;
this->next = NULL;
}
};
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: void
*/
void reorderList(ListNode *head) {
// write your code here
if (head == NULL)
return;
vector<ListNode*> v;
ListNode *p = head;
while (p != NULL)
{
v.push_back(p);
p = p->next;
}
int len = v.size();
int l = 0, r = len - 1;
while (l < r)
{
v[l]->next = v[r];
v[r--]->next = v[++l];
}
v[l]->next = NULL;
}
};
#endif