逆转单链表

问题

逆转单链表

思路

cur = pre->next;
post = cur=>next;
cur->next = pre;
cur = post;

代码 

逆转单链表
ListNode* reverseList(ListNode *root)
{
    if (root == NULL)
        return root;
    ListNode *cur = root;
    ListNode *pre = NULL;
    ListNode *post = NULL;
    ListNode *revRoot = NULL;
    while (cur != NULL)
    {
        post = cur->next;
        if (post == NULL)
            revRoot = cur;
        cur->next = pre;
        pre = cur;
        cur = post;
    }
    return revRoot;
}
逆转单链表

完整执行

逆转单链表 View Code

结果

1
2
0 1 2 3
3 2 1 0

 




本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3524888.html,如需转载请自行联系原作者