-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEX_move_max_num.c
48 lines (45 loc) · 1.07 KB
/
EX_move_max_num.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node* next;
};
struct node* head = NULL;
struct node* tail = NULL;
void MoveToTail();
int main(void) {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
struct node* tmp = malloc(sizeof(struct node));
scanf("%d", &tmp->val);
tmp->next = NULL;
if(i == 1)
head = tmp;
else
tail->next = tmp;
tail = tmp;
}
MoveToTail();
for(struct node* cur = head; cur != NULL; cur = cur->next)
printf("%d ", cur->val);
return 0;
}
// Your code goes here
void MoveToTail() {
// "struct node* head" and "struct node* tail" are in global
// you can use this two pointers to do this problem
int max=0;
struct node *cur=head,*max_node=NULL;
for(;cur!=NULL;cur=cur->next){
if(cur->val>max){
max=cur->val;
max_node=cur;
}
}
for(cur=head;cur->next!=max_node;cur=cur->next)
;
cur->next=max_node->next;
tail->next=max_node;
max_node->next=NULL;
}