-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path206_reverse.cpp
More file actions
40 lines (30 loc) · 740 Bytes
/
Copy path206_reverse.cpp
File metadata and controls
40 lines (30 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <iostream>
#include <sstream>
#include "parse_input.h"
// O(N)
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while(curr){
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
int main(){
std::string line;
std::cout << "Space separated values of l: ";
std::getline(std::cin, line);
ListNode* l = str2List(line);
ListNode* l_res = Solution().reverseList(l);
std::string answer;
List2str(l_res, answer);
std::cout << "Reversed: " << answer << std::endl;
return 0;
}