Reverse Linked List

 

CREATED TIME: Feb 17, 2020 10:39 PM UPDATED TIME: Feb 17, 2020 10:39 PM 난이도: Easy

/** * 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* before = NULL; while(head != NULL){ ListNode* tmp = head->next; head -> next = before; before = head; head = tmp; } return before; } };