题解 | 牛牛的链表交换
牛牛的链表交换
https://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc
#include <stdio.h>
#include <stdlib.h>
// write your code here......
struct node
{
int data;
struct node* Next;
};
struct node* list_create(int arr[],int n){
struct node* list=(struct node*)malloc(sizeof(struct node));
struct node* p=list;
for(int i=0; i<n; i++){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data=arr[i];
p->Next=newnode;
p=p->Next;
}
p->Next=NULL;
return list;
}
void list_out(struct node* list)
{
if(list==NULL){
return ;
}
for(struct node* p=list->Next;p;p=p->Next){
printf("%d ",p->data);
}
}
void qianhuan(struct node* list)
{
if(list==NULL || list->Next==NULL || list->Next->Next==NULL)
{
return;
}
struct node* first=list->Next;
struct node* second=first->Next;
first->Next=second->Next;
second->Next=first;
list->Next=second;
}
void houhuan(struct node* list)
{
if(list==NULL || list->Next==NULL || list->Next->Next==NULL)
{
return;
}
struct node* prev= list;
struct node* current= list->Next;
while(current->Next->Next!=NULL)
{
prev=current;
current=current->Next;
}
struct node* last=current->Next;
current->Next=NULL;
last->Next=current;
prev->Next=last;
}
int main() {
int n;
scanf("%d",&n);
int* arr=(int*)malloc(n*sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d",&arr[i]);
}
// write your code here......
struct node* list=list_create(arr,n);
qianhuan(list);
houhuan(list);
list_out(list);
free(arr);
return 0;
}
#硬件人求职现状##大学四年该怎么过,才不算浪费时间?#