题解 | #牛牛的链表交换#
牛牛的链表交换
https://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc
#include <stdio.h> #include<malloc.h> typedef struct node { int data; struct node* next; } node; //交换a和a->next位置 node* swap(node* a) { node* temp = a->next; a->next = a->next->next; temp->next = a; return temp; } int main() { int n, m; node* head = malloc(sizeof(node)); node* t = head; scanf("%d", &n); m = n; while (m--) { scanf("%d", &t->data); node* node = malloc(sizeof(node)); t->next = node; t = node; } head = swap(head); for (int i = 0; i < n; i++) { if (i == n - 2)head = swap(head); printf("%d ", head->data); head = head->next; } return 0; }#C#
0基础学C 文章被收录于专栏
0基础学C,从算法开始