单链表的反转
时间:2015-05-14 13:42:46
收藏:0
阅读:104
方法一:
新建一个单链表,遍历源链表。每次将源链表中的节点插到新链表的第一个节点位置
struct Node { int data; Node * next; }; //带表头单链表 //新建一个单链表,遍历源链表。每次将源链表中的节点插到新链表的第一个节点位置 Node * reverselist(Node * list){ Node *cur,*newList,*tmp; cur = list->next; newList = new Node; newList->next=NULL; while (cur!=NULL) { tmp = new Node; tmp->data = cur->data; tmp->next = newList->next; newList->next = tmp; cur = cur->next; } return newList; };
评论(0)