头插法

三种方法总结

struct ListNode *createlist()
{
	struct ListNode  *head = NULL, *now = NULL;
	int num;
	while(1)
	{
		scanf("%d",&num);
		if(num == -1){
			return head;
		}
		now = (struct ListNode *)malloc(sizeof(struct ListNode));
		now->data = num;
		now->next = head;
		head = now;
	}
}
struct ListNode *createlist()
{
	struct ListNode *now, *head,*q;
	now=head=NULL;
	q = (struct ListNode*)malloc(sizeof(struct ListNode));
	now = q;
	now->next=head;
	while(1)
	{	
		scanf("%d",&now->data);
		if(q->data == -1)
		{
			return head->next;
		}
		q = (struct ListNode*)malloc(sizeof(struct ListNode));
		now=q;
		now->next=head;
		head=now;	
	} 
} 
struct ListNode *createlist()
{
	struct ListNode *now, *head,*q;
	now=head=NULL;
	while(1)
	{
		q = (struct ListNode*)malloc(sizeof(struct ListNode));
		now=q;
		now->next=head;	
		scanf("%d",&now->data);
		if(q->data == -1)
		{
			return head->next;
		}
		head=now;	
	} 
}
发布了108 篇原创文章 · 获赞 114 · 访问量 8589

猜你喜欢

转载自blog.csdn.net/weixin_45773503/article/details/103835453