当前位置:网站首页>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;
}
边栏推荐
- The difference between & & and &
- [software tools] screen recording software captura
- 经典图论,深度优先和广度优先,拓扑,Prim和Krukal,该来温习啦
- leetcode - 460. LFU 缓存
- Not eligible for getting processed by all beanpostprocessors
- ActiveMQ简单教程,适合初学者,学习笔记yyds
- Web design and website planning assignment 11 game selection form
- (一)aac开篇-核心组件原理之Lifecycle、LiveData、ViewModel与源码分析技巧(转载)
- Interprocess communication
- (resolved) the tqdm progress bar in the Jupiter notebook does not update and display in one line, but scrolls down to output
猜你喜欢

How to do well in empty state design? Look at this comprehensive summary

Cyclic sort

一些学习记录i=

XXL task executor calls local project

Installation (detailed illustration) and use of SVN

Empty difference between postgrepsql and Oracle

Qiao lerna: lerna auxiliary tool

ActiveMQ简单教程,适合初学者,学习笔记yyds

Redis cluster in Linux system

Anaconda+tensorflow most effective summary version (blood and tears summary of 6 reloads)
随机推荐
AttributeError: module ‘tensorflow. compat. v2.__ internal__‘ has no attribute ‘register_ clear_ session_
【node】npm部分
Mongodb--- automatically delete expired data using TTL index
Implementation of CRF for named entity recognition
Hibernate L2 cache
JS basic learning script
Typescript unknown type
MySQL upgrade
Difference between threadpooltaskexecutor and ThreadPoolExecutor
Collation of open source modulation identification data set
Zookepper===> animal management system
Is the result too different from the goal? With the help of target management, you can reach the target accurately!
JS learning basics document Write write a line of text in the page
Go language: string connection, digital conversion string
go for it Easily manage all types of items with "flying items"
How to do well in empty state design? Look at this comprehensive summary
Using flying items to manage by objectives, not being a "headless fly" in the workplace
GO语言:字符串的连接、数字转化字符串
How to make hyperlinks in RichTextBox- How can I make a hyperlink work in a RichTextBox?
Typescript namespace