首页 > 代码库 > Reverse Linked List

Reverse Linked List

Reverse a singly linked list.

/** * 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* newHead = new ListNode(-1);        ListNode* cur = head;        while(cur){            ListNode* newHead_next = newHead->next;            ListNode* next = cur->next;            newHead->next = cur;            cur->next = newHead_next;            cur = next;        }        return newHead->next;    }};

 

Reverse Linked List