学习目标:反转一个单链表。
提示:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
/**
* 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) {
struct ListNode temp;
temp.next = NULL;
while(head){
struct ListNode *next=head->next;
head->next = temp.next;
temp.next = head;
head = next;
}
return temp.next;
}
};