题解 | #牛牛的链表交换#
牛牛的链表交换
https://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc
#include <stdio.h>
typedef struct Node{
int data;
struct Node* next;
}node;
node* cai(int v){
node* k=(node*)malloc(sizeof(node));
k->data=v;
k->next=NULL;
return k;
}
int main() {
int n;
scanf("%d",&n);
node* head=NULL;
node* tail=NULL;
for(int i=0;i<n;i++){
int v;
scanf("%d",&v);
node* f=cai(v);
if(head==NULL){
head=f;
tail=f;
}else{
tail->next=f;
tail=f;
}
}
// node* kk=head->next->next;
// node* k=head;
// head=head->next;
// head->next=k;
// k->next=kk;
node* k=head->next;
head->data=head->data^k->data;
k->data=head->data^k->data;
head->data=head->data^k->data;
node* kk=head;
while(kk->next->next!=NULL){
kk=kk->next;
}
tail->data=tail->data^kk->data;
kk->data=tail->data^kk->data;
tail->data=tail->data^kk->data;
node* p=head;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
p=head;
while(p!=NULL){
node* t=p;
p=p->next;
free(t);
}
return 0;
}
