206. Reverse Linked List

problem

206. Reverse Linked List

code

Iteration

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = NULL;
        ListNode* cur = head;
        while(cur)
        {
            ListNode* tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};
View Code

 

 

参考

1. Leetcode_Reverse Linked List

posted on 2018-12-14 10:33  鹅要长大  阅读(148)  评论(0)    收藏  举报

导航