6. Reverse Linked List 逆转单链表

逆转单链表,比较简单,不细讲,扫描依次改变指针指向。

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==nullptr)return head;
        ListNode * tmp = head->next;
        ListNode *prenode = head;
        while(tmp!=NULL)
        {  
            ListNode *nextnode = tmp->next;
            tmp->next = prenode;
            prenode = tmp;
            tmp = nextnode;
         }
         head->next = NULL;
         head = prenode;
         return head;
    }
};
posted @ 2016-04-16 22:40  Free_Open  阅读(219)  评论(0编辑  收藏  举报