当前位置:网站首页>CodeTop - 排序奇升偶降链表
CodeTop - 排序奇升偶降链表
2022-06-11 08:28:00 【zmm_mohua】
CodeTop - 排序奇升偶降链表
题目

代码
#include <iostream>
#include <vector>
using namespace std;
/* 给定一个奇数位升序,偶数位降序的链表,将其重新排序(升序) 例:1->8->3->6->5->4->7->2->NULL 1->2->3->4->5->6->7->8->NULL 解法: 1)首先安装奇偶顺序将链表进行拆分(1->3->5->7->NULL和8->6->4->2->NULL) 2)反转偶链表(1->3->5->7->NULL和2->4->6->8->NULL) 3)合并两个链表 */
typedef struct ListNode{
int val;
struct ListNode *next;
}ListNode, *LinkList;
void create(LinkList &head){
int n;
cin>>n;
head = new ListNode;
head->next = NULL;
ListNode *tail = head;
for(int i = 0; i < n; i++){
ListNode *p = new ListNode;
cin>>p->val;
p->next = NULL;
tail->next = p;
tail = p;
}
}
// 拆分奇偶链表
pair<ListNode*, ListNode*> partition(ListNode *head){
// 奇链表初始化
ListNode *oddhead = new ListNode;
oddhead->next = NULL;
ListNode *oddtail = oddhead;
// 偶链表初始化
ListNode *evenhead = new ListNode;
evenhead->next = NULL;
ListNode *eventail = evenhead;
int i = 1; // 记录链表顺序
while(head){
ListNode *p = new ListNode;
p->val = head->val;
p->next = NULL;
if(i % 2 == 1){
oddtail->next = p;
oddtail = p;
}else{
eventail->next = p;
eventail = p;
}
head = head->next;
i++;
}
oddhead = oddhead->next;
evenhead = evenhead->next;
return {
oddhead, evenhead};
}
// 反转链表
ListNode* reverseLink(ListNode *head){
ListNode *pre = NULL;
ListNode *cur = head;
while(cur){
ListNode *temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
// 合并链表
ListNode* merge(ListNode *l1, ListNode *l2){
if(!l1 || !l2){
return !l1 ? l2 : l1;
}
ListNode *head = new ListNode;
ListNode *h = head;
while(l1 && l2){
if(l1->val < l2->val){
h->next = l1;
l1 = l1->next;
}else{
h->next = l2;
l2 = l2->next;
}
h = h->next;
}
if(l1){
h->next = l1;
}
if(l2){
h->next = l2;
}
return head->next;
}
ListNode* sortOddEvenList(ListNode *head){
if(!head || !head->next){
return head;
}
pair<ListNode*, ListNode*> res = partition(head);
ListNode *odd = res.first;
ListNode *even = res.second;
even = reverseLink(even);
head = merge(odd, even);
return head;
}
int main(){
ListNode *head, *res;
create(head);
head = head->next;
res = sortOddEvenList(head);
while(res){
cout<<res->val<<" ";
res = res->next;
}
return 0;
}
边栏推荐
- Basic use of typescripy class
- Planning tasks for continuous automated testing
- [cvpr2022] intensive reading of querydet papers
- node报错整理
- Dameng database startup and shutdown
- (taking pytorch as an example) a simple understanding of the regularization method of path (depth) -drop path
- Use special characters to splice strings "+“
- (the slow download speed of cifar10 in torchvision has been solved) how to download and use torchvision import
- Sign in system design: how to draw the sign in function
- SylixOS SD设备驱动开发
猜你喜欢

torch. Var (), sample variance, parent variance

知识图谱入门之---yedda标注

Solution to the occurrence interval (space) of latex manual numbering documents

不想项目失控?你需要用对项目管理工具

堆是也可以看成一种树结构,规定根节点必须大于或小于左右子节点,但左右子节点的大小顺序没有规定

Introduction to database system experiment report answer Experiment 5: database single table query

Jupyter notebook code completion plug-in + Solution

用飞项进行目标管理,不做职场上的“无头苍蝇”

Use of Excel to XML tool of TestLink

uniapp 插件开发
随机推荐
Js学习基础document.write在页面中写一行文字
【CVPR2022】QueryDet论文精读
(resolved) the tqdm progress bar in the Jupiter notebook does not update and display in one line, but scrolls down to output
深度学习入门之pytorch安装
Redis6 entry-level tutorial. There are integration cases. You can directly see the integration cases. It is easy to get started
SSM file upload and download
Idea pulls items from remote warehouse
Multiple limit of the same field of SQL
Modulenotfounderror: no module named 'tensorboard in pytorch‘
命名实体识别之CRF的实现方式
torch. nn. functional. pad
Anaconda related knowledge supplement (spyder+keras Library)
堆是也可以看成一种树结构,规定根节点必须大于或小于左右子节点,但左右子节点的大小顺序没有规定
go for it Easily manage all types of items with "flying items"
Zookepper===>动物管理员系统
Qiao NPMS: get the download volume of NPM packages
Anaconda+tensorflow most effective summary version (blood and tears summary of 6 reloads)
Typescript distributed condition type
Icml2022 article intéressant
torch. Var (), sample variance, parent variance