题解 | #牛牛的链表交换#

牛牛的链表交换

https://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc

#include<stdio.h>
#include<stdlib.h>
#define bool char
#define true 1
#define false 0
typedef struct node
{
	int data;
	struct node* next;
}node, * linklist;
bool InsertLink(linklist l, int* i)
{
	node* p, * s;
	p = l;
	while (p->next != NULL)
	{
		p = p->next;
	}
	s = (node*)malloc(sizeof(node));
	s->data = i;
	p->next = s;
	s->next = NULL;
	p = s;
	return true;
}
bool excnode(linklist l, int i)
{
	node* p, * s;
	if (i < 2)
		return false;
	p = l->next;
	p->data = p->data + p->next->data;
	p->next->data = p->data - p->next->data;
	p->data = p->data - p->next->data;
	while (p->next != NULL)
	{
		if (p->next->next == NULL)
		{
			p->data = p->data + p->next->data;
			p->next->data = p->data - p->next->data;
			p->data = p->data - p->next->data;
		}
		p = p->next;
	}
	return true;
}
void Prin(linklist l)
{
	node* p;
	p = l;
	while (p->next != NULL)
	{
		p = p->next;
		printf("%d ", p->data);
	}
	printf("\n");
}
int main()
{
	linklist l;
	int n, * arr;;
	l = (node*)malloc(sizeof(node));
	l->next = NULL, l->data = 0;
	scanf("%d", &n);
	arr = (int*)malloc(sizeof(int) * n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
		InsertLink(l, *(arr + i));
	}
	excnode(l, n);
	Prin(l);
	return 0;
}

全部评论

相关推荐

斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务