当前位置:网站首页>Codetop - sort odd ascending even descending linked list
Codetop - sort odd ascending even descending linked list
2022-06-11 08:43:00 【zmm_ mohua】
CodeTop - Sort odd ascending even descending linked list
subject

Code
#include <iostream>
#include <vector>
using namespace std;
/* Given an odd digit ascending order , Even bit descending list , Reorder them ( Ascending ) example :1->8->3->6->5->4->7->2->NULL 1->2->3->4->5->6->7->8->NULL solution : 1) First, install the parity sequence to split the linked list (1->3->5->7->NULL and 8->6->4->2->NULL) 2) Reverse even linked list (1->3->5->7->NULL and 2->4->6->8->NULL) 3) Merge two linked lists */
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;
}
}
// Split the parity linked list
pair<ListNode*, ListNode*> partition(ListNode *head){
// Odd list initialization
ListNode *oddhead = new ListNode;
oddhead->next = NULL;
ListNode *oddtail = oddhead;
// Even linked list initialization
ListNode *evenhead = new ListNode;
evenhead->next = NULL;
ListNode *eventail = evenhead;
int i = 1; // Record linked list order
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};
}
// Reverse a linked list
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;
}
// Merge list
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;
}
边栏推荐
- 进程间的通信
- MySQL & Oracle database capacity query
- 知识图谱入门之---yedda标注
- 二、渲染 Camera 的数据
- The role of lambdalr in pytorch
- uniapp 插件开发
- Heap can also be regarded as a tree structure. It is specified that the root node must be greater than or less than the left and right child nodes, but the size order of the left and right child nodes
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- Js基础学习Script
- Award winning survey | how Apache pulsar lived in 2022, you the final say
猜你喜欢

Is the result too different from the goal? With the help of target management, you can reach the target accurately!

Using flying items to manage by objectives, not being a "headless fly" in the workplace

标准化编写知识

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

Web design and website planning assignment 14 add background music to the video

centos随笔03:centos8.2安装mysql

Award winning survey | how Apache pulsar lived in 2022, you the final say

Idea pulls items from remote warehouse

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

torch. meshgrid
随机推荐
qiao-lerna:lerna辅助工具
[software tools] screen recording software captura
【CVPR2022】QueryDet论文精读
(一)aac开篇-核心组件原理之Lifecycle、LiveData、ViewModel与源码分析技巧(转载)
AttributeError: module ‘tensorflow. compat. v2.__ internal__‘ has no attribute ‘register_ clear_ session_
MySQL upgrade
Interprocess communication
知识图谱入门之---yedda标注
Use of Excel to XML tool of TestLink
Introduction to knowledge atlas -- yedda annotation
Solve cannot import name 'multiheadattention' from 'tensorflow keras. layers‘
SSM file upload and download
深度学习入门之pytorch安装
剑指 Offer 62. 圆圈中最后剩下的数字
MySQL advanced features, you can read more about it and meet the interview
Asynchronous notification mechanism of character device driver
torch. roll
ICML2022有意思的文章
剑指 Offer 51. 数组中的逆序对
Deep understanding of add in argparse module_ Argument parameters (such as action)