美文网首页
206. Reverse Linked List

206. Reverse Linked List

作者: YellowLayne | 来源:发表于2017-06-27 10:24 被阅读0次

    1.描述

    Reverse a singly linked list.

    2.分析

    3.代码

    /**
     * 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) {
            if (head == NULL || head->next == NULL) return head;
            ListNode* cur = head;
            ListNode* pre;
            head = NULL;
            while (cur != NULL) {
                pre = cur;
                cur = cur->next;
                pre->next = head;
                head = pre;
            }
            return head;        
        }
    };
    

    相关文章

      网友评论

          本文标题:206. Reverse Linked List

          本文链接:https://www.haomeiwen.com/subject/tegscxtx.html