列表

详情


CC8. 牛牛的链表交换

描述

牛牛尝试把一个长度为 n 的数组转换成链表并把链表前两个节点交换位置和把链表最后两个节点交换位置。

输入描述

第一行输入一个正整数 n 表示数组的长度
第二行输入 n 个正整数,表示数组中各个元素的值

输出描述

把数组转换成链表后输出交换位置后的链表

示例1

输入:

4
2 3 4 5

输出:

3 2 5 4

示例2

输入:

3
3 2 1

输出:

2 1 3

原站题解

C 解法, 执行用时: 2ms, 内存消耗: 296KB, 提交时间: 2022-05-22

int main()
{
    int n=0;
    scanf("%d",&n);
    int arr[n];
    int i=0;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int *p=arr;
    int t=0;
    t=*p;
    *p=*(p+1);
    *(p+1)=t;
    t=*(p+n-1);
    *(p+n-1)=*(p+n-2);
    *(p+n-2)=t;
    for(i=0;i<n;i++)
    {
        printf("%d ",*(p+i));
    }
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 300KB, 提交时间: 2022-07-30

#include <stdio.h>
#include <malloc.h>
#include<stdlib.h>
typedef struct Node
{
  int data;
  struct Node* pNext ; 
} Node, *pNode;

int main()
{
    int len = 0;
    scanf("%d",&len);
    pNode phead = (pNode)malloc(sizeof(Node));
    pNode ptrail= (pNode)malloc(sizeof(Node));
	phead = ptrail;
    for(int i = 0; i < len; i++)
    {
        pNode pNew = (pNode)malloc(sizeof(Node));
        scanf("%d",&pNew->data);
        ptrail->pNext = pNew;
        pNew->pNext = NULL;
        ptrail = pNew;
    }
    int temp = 0;
    pNode p = phead->pNext;
    temp = p->data;
    p->data = p->pNext->data;
    p->pNext->data = temp;
    while(len-2 != 0)
    {
        p = p->pNext;
        len--;
    }
    temp = p->data;
    p->data = p->pNext->data;
    p->pNext->data = temp;
    pNode q = phead->pNext;
	while(q != NULL)
	{
		 printf("%d ",q->data);
		   q = q->pNext;
	}
}

C 解法, 执行用时: 2ms, 内存消耗: 300KB, 提交时间: 2022-06-30

#include <stdio.h>
#include <stdlib.h> 
typedef struct _ListNode{
    int val;
    struct _ListNode* next;
}ListNode;
 int main(void){
     int n;
     scanf("%d",&n);
     ListNode* out = (ListNode*)malloc(sizeof(ListNode));
     ListNode* temp = out;
     ListNode* temp2 = out;
     for(int i=0;i<n;i++){
         scanf("%d",&temp->val);
         temp->next = (ListNode*)malloc(sizeof(ListNode));
         temp = temp->next;
     }
     for(int i=0;i<n;i++){
         if(i==0||i==n-2){
             int temp_int = temp2->next->val;
             temp2->next->val = temp2->val;
             temp2->val = temp_int;
         }
         printf("%d ",temp2->val);
         temp2 = temp2->next;
         
         
     }
 
 }

C 解法, 执行用时: 2ms, 内存消耗: 304KB, 提交时间: 2022-03-18

#include<stdio.h>
#define N 1000
struct node
{
	int num;
	struct node* next;
};
int a[N];
int Len = sizeof(struct node);
struct node *creat(int n)
{
	struct node *head=NULL,*p1=NULL,*p2=NULL;
	p1 = (struct node*)malloc(Len);
	p1->next = NULL;
	head = p1;
	for (int i = 0; i < n; i++)
	{
		p2 = (struct node*)malloc(Len);
		p2->num = a[i];
		p1->next = p2;
		p1 = p2;
	}
	p1->next = NULL;
	return(head);
}
void list(struct node *head)
{
	struct node* p1=head;
	struct node* s = NULL;
	if (head == NULL)
	{
		printf("该链表为空无法打印");

	}
	else
	{
		s= p1->next;
		while (s != NULL)
		{
			printf("%d ", s->num);
			s =s->next;
		}
	}
}
struct node *change(struct node *head,int n)
{
	int flag;
	int i = 0;
	struct node *p1 = NULL, *p2 = NULL, *p3 = NULL;
	if (head == NULL)
	{
		printf("该链表为空无法交换\n");

	}
	else
	{
		p1 = head;
		for (i = 0; i < n; i++)
		{
			p1 = p1->next;
			if (i == 0)
			{
				flag = p1->num;
				p1->num = p1->next->num;
				p1->next->num = flag;
			}
			if (i == n - 2)
			{
				flag = p1->num;
				p1->num = p1->next->num;
				p1->next->num = flag;
			}
		}

	}
	return(head);

}
int main()
{
	int n;
	scanf("%d", &n);
	int i = 0;
	struct node *head;
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);

	}
	head = creat(n);
	head = change(head,n);
	list(head);
}

C 解法, 执行用时: 2ms, 内存消耗: 308KB, 提交时间: 2022-03-09

#include <stdio.h>
#include <stdlib.h>
struct Node
{
    size_t n;
    struct Node *next;
};
void swap(size_t *a, size_t *b)
{
    size_t t = *a;
    *a = *b;
    *b = t;
}
int main()
{
    size_t n;
    struct Node list;
    list.next = NULL;
    scanf("%d", &n);
    for (struct Node *node = &list; n--; node = node->next)
    {
        node->next = (struct Node *)malloc(sizeof(struct Node));
        node->next->next = NULL;
        scanf("%lld", &node->next->n);
    }
    struct Node *l = list.next, *r = list.next->next;
    if (r)
    {
        swap(&l->n, &r->n);
        while (r->next)
        {
            l = l->next;
            r = r->next;
        }
        swap(&l->n, &r->n);
    }
    for (struct Node *node = list.next; node; node = node->next)
        printf("%lld ", node->n);
}

上一题