赞
踩
反转链表很经典,常出现在校招面试中,出现频率top1。
反转链表校招热门题型
思路一:翻指针(翻指针的方向)如图:
执行过程如图:
- struct ListNode* reverseList(struct ListNode* head){
- if(head==NULL)
- return NULL;
- else
- {
- struct ListNode* n1=NULL,*n2=head,*n3=n2->next;
- //结束条件
- while(n2!=NULL)
- {
- //迭代过程
- n2->next=n1;
- n1=n2;
- n2=n3;
- if(n3!=NULL)
- {n3=n3->next;}
- }
- return n1;
-
- }
- }

思路二:头插法
- struct ListNode* reverseList(struct ListNode* head){
- struct ListNode *cur=head,*newhead=NULL;
- while(cur!=NULL)
- {
- struct ListNode* next=cur->next;
- cur->next=newhead;
- newhead=cur;
- cur=next;
- }
- return newhead;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。