BM12 单链表的排序
给定一个节点数为n的无序单链表,对其按升序排序。
数据范围:0<n≤1000000<n≤100000,保证节点权值在[−109,109][−109,109]之内。
要求:空间复杂度 O(n)O(n),时间复杂度 O(nlogn)O(nlogn)
思考:
链表转数组,然后快排,
这道题目就是让大家重新手写一下快排
代码:
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
#include <algorithm>
#include <vector>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head node
* @return ListNode类
*/
ListNode* sortInList(ListNode* head) {
// write code here
ListNode* cur = head;
int len = 0;
while (cur) {
cur = cur->next;
len++;
}
int a[len + 1];
cur = head;
for (int i = 0;i < len;i++, cur = cur->next) a[i] = cur->val;
std::sort(a,a + len);
ListNode* p = new ListNode(-1);
ListNode* res = p;
for (int i = 0;i < len;i++) {
ListNode* tmp = new ListNode(-1);
tmp->val = a[i];
tmp->next = nullptr;
p->next = tmp;
p = p->next;
}
return res->next;
}
};
快排代码:
void quickSorted(int *nums, int left, int right){
if (left > right) return;
int i = left, j = right, temp = nums[left];
while (i < j){
while (i < j && temp <= nums[j])
j--;
if (i < j)
nums[i++] = nums[j];
while (i < j && temp > nums[i])
i++;
nums[j--] = nums[i];
}
nums[i] = temp;
quickSorted(nums, left, i - 1);
quickSorted(nums, i + 1, right);
}
BM13 判断一个链表是否为回文结构
给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
数据范围: 链表节点数 0≤n≤1050≤n≤105,链表中每个节点的值满足 ∣val∣≤107∣val∣≤107
思考:
将链表元素放置在一个数组中,对数组进行双指针双端遍历,直到两个指针相遇。
如果此时没有不同的元素,即为回文结构。
代码:
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
int len = 0;
ListNode* p = head;
for (;p;p = p->next) len++;
int a[len+1];
p = head;
for (int i = 0;i < len;i++,p = p->next) a[i+1] = p->val;
// bool flag = true;
if (len == 2) {
return a[1] == a[2] ? true : false;
}
for (
int i = 1, j = len; i != j && i + 1 != j;i++, j--
) {
if (a[i] != a[j]) return false;
}
return true;
}
};
BM14 链表的奇偶重排
给定一个单链表,请设定一个函数,将链表的奇数位节点和偶数位节点分别放在一起,重排后输出。
注意是节点的编号而非节点的数值。
数据范围:节点数量满足 0≤n≤1050≤n≤105,节点中的值都满足 0≤val≤10000≤val≤1000
要求:空间复杂度 O(n)O(n),时间复杂度 O(n)O(n)
思考:
遍历链表,分别将奇偶数位元素以尾插法插入至不同的链表中,然后将两个链表拼接在一起
代码:
/**
* 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
ListNode* cur = head;
ListNode* list1 = new ListNode(-1);
ListNode* list2 = new ListNode(-2);
// list1->next = nullptr;
// list2->next = nullptr;
ListNode* p1 = list1;
ListNode* p2 = list2;
int len = 1;
while (cur) {
if (len %2 == 1)
{
ListNode* tmp = new ListNode(0);
tmp->val = cur->val;
tmp->next = nullptr;
p1->next = tmp;
p1 = p1->next;
}
else {
ListNode* tmp = new ListNode(0);
tmp->val = cur->val;
tmp->next = nullptr;
p2->next = tmp;
p2 = p2->next;
}
cur = cur->next;
len++;
}
p1->next = list2->next;
return list1->next;
}
};