单链表的逆转

来源:互联网 发布:win7仿mac dock栏 编辑:程序博客网 时间:2024/06/06 20:44

单链表的逆转

 利用3个指针遍历单链表,逐个链接点进行反转。

/*只需要完成逆置链表函数struct ListNode{int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/class Solution {public:ListNode* ReverseList(ListNode* pHead){if(pHead==NULL||pHead->next==NULL) return pHead;ListNode*p,*q,*r;p = pHead;    q = pHead->next;  pHead->next = NULL;while(q){  r = q->next;q->next = p; p = q;   q = r;   }  pHead=p; return pHead;      }};


参考:http://blog.csdn.net/feliciafay/article/details/6841115

原创粉丝点击