链表的倒置
发布时间:2020-12-14 05:22:32 所属栏目:百科 来源:网络整理
导读:对于链表的倒置我们可以采用(1)排序方法将所有的数据顺序颠倒,如冒泡法 (2)也可以仅仅将链表中的next指针改变,如下 #include iostreamusing namespace std;struct node{int data;node* next;};node* reverse(node* head){if(head==NULL||head-next==NULL
对于链表的倒置我们可以采用(1)排序方法将所有的数据顺序颠倒,如冒泡法 (2)也可以仅仅将链表中的next指针改变,如下
#include <iostream> using namespace std; struct node { int data; node* next; }; node* reverse(node* head) { if(head==NULL||head->next==NULL) return head; node* p1,*p2,*p3; p1=head; p2=p1->next; while(p2) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } head->next=NULL; head=p1; return head; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |