Title: Reverse Linked List
- Topic description:
Reverse a singly linked list.
Advanced:
Linked lists can be reversed iteratively or recursively. Can you do both?
/**
* 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 *new_head = NULL; //指向新链表头节点的指针
while(head){
ListNode *next = head->next; //备份head->next
head->next = new_head; //更新head->next
new_head = head; //移动new_head
head = next; //遍历链表
}
return new_head; //返回新链表头节点
}
};