LeetCode(206)
通过代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LNode;
typedef struct ListNode *LNode_Pointer;
struct ListNode* reverseList(struct ListNode* head)
{
LNode_Pointer head_old_temp,tail,tail_prior;
head_old_temp=(LNode_Pointer)malloc(sizeof(LNode));
head_old_temp->next=head;
tail=head;
tail_prior=head_old_temp;
//我觉得又有必要申请一个新的头结点了。我觉得没有头结点真的挺麻烦的。在判断条件上。
LNode_Pointer head_temp,tail_temp;
head_temp=(LNode_Pointer)malloc(sizeof(LNode));
head_temp->next=NULL;
tail_temp=head_temp;
while(head_old_temp->next!=NULL)
{
tail_prior=head_old_temp;
tail=head_old_temp->next;
while(tail->next!=NULL)
{
tail_prior=tail;
tail=tail->next;
}
{
LNode_Pointer q=(LNode_Pointer)malloc(sizeof(LNode));
q->val=tail->val;
//如果是申请的结点,那就应该用.来引用对象的成员。不对。返回的是指针。所以不能用LNode q=(LNode)malloc(sizeof(LNode));
tail_temp->next=q;
tail_temp=tail_temp->next;
tail_temp->next=NULL;
tail_prior->next=tail->next;
free(tail);
tail=NULL;
}
}
return head_temp->next;
}
提交结果:
这效率有点低。有时间再优化(那么问题来了,我们什么时候才会有时间呢)。
完整代码:
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
typedef struct ListNode LNode;
typedef struct ListNode *LNode_Pointer;
struct ListNode* reverseList(struct ListNode* head)
{
LNode_Pointer head_old_temp,tail,tail_prior;
head_old_temp=(LNode_Pointer)malloc(sizeof(LNode_Pointer));
head_old_temp->next=head;
tail=head;
tail_prior=head_old_temp;
//我觉得又有必要申请一个新的头结点了。我觉得没有头结点真的挺麻烦的。在判断条件上。
LNode_Pointer head_temp,tail_temp;
head_temp=(LNode_Pointer)malloc(sizeof(LNode));
head_temp->next=NULL;
/*head_old_temp=(LNode_Pointer)malloc(sizeof(LNode_Pointer));malloc的参数写错了
*/
tail_temp=head_temp;
while(head_old_temp->next!=NULL)
{
tail_prior=head_old_temp;
tail=head_old_temp->next;
while(tail->next!=NULL)
{
tail_prior=tail;
tail=tail->next;
}
//else
{
LNode_Pointer q=(LNode_Pointer)malloc(sizeof(LNode));
q->val=tail->val;
//如果是申请的结点,那就应该用.来引用对象的成员。不对。返回的是指针。所以不能用LNode q=(LNode)malloc(sizeof(LNode));
tail_temp->next=q;
tail_temp=tail_temp->next;
tail_temp->next=NULL;
tail_prior->next=tail->next;
free(tail);
tail=NULL;
}
}
return head_temp->next;
}
void Tail_Insert_List(LNode_Pointer &head,LNode_Pointer &tail,int val)//&tail。不加&的话每次的tail的值还是head的值
{
LNode_Pointer q=(LNode_Pointer)malloc(sizeof(LNode));
if(tail!=NULL)
{
q->val=val;
tail->next=q;
tail=tail->next;
tail->next=NULL;
}
else
{
q->val=val;
tail=q;
tail->next=NULL;
head=tail;//注意这里。不这样的话主函数的head仍然是0。
}
}
void printf_List(LNode_Pointer p)
{
while(p!=NULL)
{
if(p->next!=NULL)
{
//printf("%d(地址=%p)->",p->val,p);
printf("%d->",p->val,p);
p=p->next;
}
else
{
//printf("%d(地址=%p)",p->val,p);
printf("%d",p->val,p);
p=p->next;
}
}
}
int main()
{
int val;
LNode_Pointer head=(LNode_Pointer)malloc(sizeof(LNode));
head=NULL;
LNode_Pointer tail=(LNode_Pointer)malloc(sizeof(LNode));
tail=head;
while(scanf("%d",&val)!=EOF)
{
Tail_Insert_List(head,tail,val);
}
printf_List(head);
printf("\n");
LNode_Pointer head_return=reverseList(head);
printf_List(head_return);
return 0;
}
本地运行结果: