-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse a linked list.cpp
43 lines (36 loc) · 1.1 KB
/
Reverse a linked list.cpp
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
41
42
43
// Reverse a linked list
// Difficulty: EasyAccuracy: 73.11%Submissions: 294K+Points: 2
// Given head a linked list, the task is to reverse this list.
// The following is internal representation of every test case (two inputs).
// n : Size of the linked list
// value[] : An array of values that represents values of nodes.
// Examples:
// Input: n = 6, value[] = {1, 2, 3, 4, 5, 6}
// Output: 6 5 4 3 2 1
// Explanation:
// Input: n = 5, value[] = {2, 7, 10, 9, 8}
// Output: 8 9 10 7 2
// Explanation:
// Input: n = 1, value[] = {10}
// Output: 10
// Explanation: For a single node list, the reverse would be same as original
// Expected Time Complexity: O(n).
// Expected Auxiliary Space: O(1).
// Constraints:
// 1 <= n <= 104
class Solution {
public:
// Function to reverse a linked list.
struct Node* reverseList(struct Node* head) {
// code here
// return head of reversed list
Node* next,* prev=nullptr;
while(head!=nullptr){
next=head->next;
head->next=prev;
prev=head;
head=next;
}
return prev;
}
};