美文网首页
206. Reverse Linked List

206. Reverse Linked List

作者: juexin | 来源:发表于2017-01-09 19:33 被阅读0次

    Reverse a singly linked list.

    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(head==NULL||head->next==NULL)
               return head;
            ListNode* pre = head;
            ListNode* cur = head->next;
            ListNode* next = head->next->next;
            pre->next = NULL;
            
            while(cur)
            {
                next = cur->next;
                cur->next = pre;
                pre = cur;
                cur = next;
            }
            return pre;
        }
    };
    

    相关文章

      网友评论

          本文标题:206. Reverse Linked List

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