当前位置:网站首页>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;
}
边栏推荐
- ICML2022有意思的文章
- torch. nn. functional. pad
- Multiple limit of the same field of SQL
- GCC AVR(Atmel Studio+ AVR Studio)如何将结构体数组定义在程序存储器(flash)空间并进行读操作
- How to do well in empty state design? Look at this comprehensive summary
- Typescript enumeration
- MySQL死锁问题如何解决?背诵版
- Zipkin入门
- js 中 Map 和 Set 的用法及区别
- BFS on tree (tree breathing first search)
猜你喜欢

B+超强树,带你知晓MySQL的底层是怎样的结构

What does it mean to buy a single-mode, dual-mode and Rechargeable Wireless Mouse

Node error report sorting

Redis cluster in Linux system

CentOS essay 03:centos8.2 installing MySQL

命名实体识别之CRF的实现方式

盘它!用「飞项」轻松管理各类型项目

进程控制:进程等待(回收子进程)

Entity class conversion cannot be reassigned

Zipkin入门
随机推荐
Web design and website planning assignment 14 add background music to the video
Collation of open source modulation identification data set
(completely solved) dataframe assignment settingwithcopywarning: a value is trying to be set on a copy of a slice
Introduction to the principles of linkedblockingqueue, arrayblockingqueue, synchronousqueue, concurrentlinkedqueue and transferqueue
torch. nn. functional. pad
Asynchronous notification mechanism of character device driver
堆是也可以看成一种树结构,规定根节点必须大于或小于左右子节点,但左右子节点的大小顺序没有规定
How to make hyperlinks in RichTextBox- How can I make a hyperlink work in a RichTextBox?
Zookepper===> animal management system
Pypharm startup is stuck, loading project
(resolved) pychart debug error -unicode decodeerror: 'UTF-8' codec can't decode byte 0xe8 in position 1023
qiao-lerna:lerna辅助工具
这几个小工具也太好用了
Entity class conversion cannot be reassigned
进程控制:进程等待(回收子进程)
How CSDN reports plagiarized articles
Typescript keyboard mapping
Difference between threadpooltaskexecutor and ThreadPoolExecutor
Study the Analects of entanglement
【CVPR2022】QueryDet论文精读