我去,单链表的逆转都没搞定,,,醉了
写的啥破玩意儿,,, 还是写不出来。
能出结果,但不知道哪里错了,没被ac, 太low了。
low写法:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/*
思路:
1.
*/
// class Solution {
// public:
// ListNode* reverseList(ListNode* head) {
// ListNode* Ppre=head;
// ListNode* Pcur=head->next;
// //ListNode* Pnext=Pcur->next;
// head->next=NULL;
// if(NULL == Pcur->next){
// Pcur->next=Ppre;
// return Pcur;
// }
// else{
// head=reverseList(Pcur);
// // head->next=NULL;
// return head;
// }
// }
// };
class Solution {
public:
int length(ListNode* head) {
int len=1;
while(NULL!=head->next)
{
len++;
head=head->next;
//printf("%d ",len);
}
return len;
}
ListNode* reverseList(ListNode* head) {
ListNode* head_orig=head;
int len=length(head);
int arry[len];
// vector<int>& nums;
for(int i=0;i<len;i++)
// while(NULL!=head)
{
arry[i]=head->val;
head=head->next;
printf("%d ",arry[i]);
}
head=head_orig;
len--;
//for(int j=0;j<len+1;j++)
while(NULL!=head)
{
printf("==%d \n",arry[len]);
head->val=arry[len--];
head=head->next;
}
return head_orig;
}
};
等下用正规的方法吧,,,
看了别人之后的写法;
/*
迭代思路:
1.空链表,或者只有一个元素,直接返回head
2.其他:
新定义几个中间变量,用头插法,
cur_head, head_next,head
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(NULL==head || NULL==head->next) return head;
ListNode *cur_head=NULL,*head_next=NULL;
while(head)
{
head_next = head->next;//save next
//头插,插在cur_head的前面
head->next= cur_head;//point to current head, insert in front of
cur_head=head;//modify the first element,cur_head 修改最前面的头的位置,以便下次头插指向
head=head_next;//修改未遍历的头
}
return cur_head;//返回最前面的头
}
};
总结:
1.名字命名的不直观会特别影响理解和思路,,,努力把名字命名得简易直接明了。
2.分类讨论,简单化问题。
3.分析能力也太差了吧,先理清具体思路,每一步,再动手写。
醉了,递归也不会写。
//make your thinking clearly before coding
/*
递归思路:
1.走到最后一个元素之后,翻转指向
2.意味着,前面的每一层都得保存着,保存的信息应该包括 ,前一个节点,
3.分三类思考:分别分析判断,
第1个节点,最后将next域指向NULL(if后)
最后一个节点,返回本节点,并将next域指向head_pre
中间的普通节点,
递去:保存pre_node,head=head->next,head_orig
归来:(条件,到最后一个元素了) cur_node=reverse(head->next),cur_node->next=pre_node
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre_save_node=NULL, *cur_node=NULL,*head_orig=NULL,*head_final=NULL;
//if(NULL==head) return head;
if( NULL==head->next)
{//到最后了,归,只来一次
head->next=pre_save_node;
// head=pre_save_node;
head_final=head;
// cur_node=head->next;
cur_node=pre_save_node;
printf("comeback:%d\n",head->val);
return cur_node;
}
pre_save_node=head;//存起来,后面作为下一个节点的next
printf("comeback:%d\n",pre_save_node->val);
cur_node=reverseList(head->next);
printf("====\n");
// printf("comeback:%d\n",cur_node->val);
if(NULL!=head_orig) cur_node->next=head;
// printf("====\n");
return head_final;
}
};
这么复杂还叫递归吗???
重新分析:
递归肯定是极度简洁的,
看别人家的:
//make your thinking clearly before coding
/*
递归思路:
1.走到最后一个元素之后,翻转指向
2.意味着,前面的每一层都得保存着,保存的信息应该包括 ,前一个节点,
3.分三类思考:分别分析判断,
第1个节点,最后将next域指向NULL(if后)
最后一个节点,返回本节点,并将next域指向head_pre
中间的普通节点,
递去:保存pre_node,head=head->next,head_orig
归来:(条件,到最后一个元素了) cur_node=reverse(head->next),cur_node->next=pre_node
*/
// class Solution {
// public:
// ListNode* reverseList(ListNode* head) {
// ListNode *pre_save_node=NULL, *cur_node=NULL,*head_orig=NULL,*head_final=NULL;
// //if(NULL==head) return head;
// if( NULL==head->next)
// {//到最后了,归,只来一次
// head->next=pre_save_node;
// // head=pre_save_node;
// head_final=head;
// // cur_node=head->next;
// cur_node=pre_save_node;
// printf("comeback:%d\n",head->val);
// return cur_node;
// }
// pre_save_node=head;//存起来,后面作为下一个节点的next
// printf("comeback:%d\n",pre_save_node->val);
// cur_node=reverseList(head->next);
// printf("====\n");
// // printf("comeback:%d\n",cur_node->val);
// if(NULL!=head_orig) cur_node->next=head;
// // printf("====\n");
// return head_final;
// }
// };
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(NULL==head || NULL==head->next) return head;
ListNode *new_head=NULL,*head_next=NULL;
head_next=head->next;
head->next=NULL;//递归前切断
new_head=reverseList(head_next);//递归
head_next->next=head;//递归后反过来链接,改变指向
return new_head;
}
};
总结:肯定是过程没分析清,对递归根本不理解。