206. Reverse Linked List

2018/04/17 leetcode 共 350 字,约 2 分钟

Title

the problem link

Reverse a singly linked list.

反转链表

Solution

//cpp:
/**
 * 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 *p1=NULL;
        
        while(head){
            ListNode *p=new ListNode(head->val);
            p->next=p1;
            p1=p;
            head=head->next;
        }
        return p1;
    }
};

Search

    Table of Contents