您的位置:首页 > 其它

链表------单向链表的反转

2016-08-21 23:27 369 查看
三指针法遍历,一次遍历则完成反转:

<span style="font-size:18px;">ListNode* ReverseList(ListNode* pHead) {
//no node
if(pHead==nullptr) return pHead;
ListNode* pre=nullptr,*mid=pHead,*last=nullptr;
//more node/single node
while(mid->next!=nullptr){
//confirm the location
last=mid->next;
mid->next=pre; pre=mid;mid=last;
}
mid->next=pre;
pHead=mid;
return pHead;
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: