leetcode 206. Reverse Linked List

头插法,每次取出后插入新链表的头部。

    ListNode* reverseList(ListNode* head) {
        if (head == NULL) return NULL;
        ListNode* ret = NULL;
        
        while (head) {
            ListNode* next = head->next;
            head->next = ret;
            ret = head;
            head = next;
        }
        return ret;
    }

 

posted on 2018-02-06 09:49  willaty  阅读(66)  评论(0编辑  收藏  举报

导航