标签:
1.使用三个节点指针遍历链表实现反转
1 template<typename Object> 2 void ReverseList(SingleListNode<Object> *head) 3 { 4 //1.三个指针迭代 5 SingleListNode<Object> *p,*q,*r; 6 if (head->next==NULL) 7 { 8 return ; 9 } 10 else 11 { 12 p=head->next; 13 q=p->next; 14 r=q->next; 15 p->next=NULL; 16 while (q) 17 { 18 r=q->next; 19 q->next=p; 20 p=q; 21 q=r; 22 } 23 head->next=p; 24 } 25 return ; 26 }
2.两个指针迭代,遍历链表将节点依次插入到头节点后,同时p,q节点指针后移
1 template<typename Object> 2 void ReverseList(SingleListNode<Object> *head) 3 { 4 //2.两个指针迭代,遍历链表将节点依次插入到头节点后,同时p,q节点指针后移 5 SingleListNode<Object> *p,*q; 6 if (head->next==NULL) 7 { 8 return; 9 } 10 else 11 { 12 p=head->next; 13 q=p->next; 14 while(q) 15 { 16 p->next=q->next; 17 q->next=head->next; 18 head->next=q; 19 q=p->next; 20 } 21 } 22 return; 23 24 }
标签:
原文地址:http://www.cnblogs.com/fengyiyangdeakaliusi/p/4675417.html