标签:node head NULL tempHead next slow 2019 数据结构 408
#include<iostream> using namespace std; struct node { int data; struct node* next; }; node* solustion(node* head) { node* fast = head, * slow = head; while ( fast != NULL&& fast->next != NULL) { if (fast == head) { fast = head->next; slow = head; } else { fast = fast->next->next; slow = slow->next; } } node* pre = slow, * nextSlow = slow->next->next; slow = slow->next; pre->next = NULL; while(nextSlow!=NULL){ slow->next = pre; pre = slow; slow = nextSlow; nextSlow = nextSlow->next; } slow->next = pre; cout << slow->data << endl; node* tempHead = head, * tempTail = slow; node* nextTempHead = tempHead->next, * nextTempTail = slow->next; while (tempHead!=NULL) { tempHead->next = tempTail; tempTail->next = nextTempHead; tempHead = nextTempHead; tempTail = nextTempTail; if (tempHead != NULL && tempTail != NULL) { nextTempHead = nextTempHead->next; nextTempTail = nextTempTail->next; } } while (head != NULL) { cout << head->data << " "; head = head->next; } return head; } int main() { int in = 0; node* head = NULL; node* tempHead = NULL, * temp = NULL; while (cin >> in) { if (head == NULL) { head = (node*)malloc(sizeof(node)); head->next = NULL; head->data = in; tempHead = head; } else { temp = (node*)malloc(sizeof(node)); temp->next = NULL; temp->data = in; tempHead->next = temp; tempHead = temp; } } solustion(head); }
在解此题的时候让我对于while或者说所有的循环有了新的理解。
标签:node,head,NULL,tempHead,next,slow,2019,数据结构,408 来源: https://www.cnblogs.com/Stone-Sea/p/16485872.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。