-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverse_a_linked_list.cpp
57 lines (48 loc) · 1.18 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Problem Statement:
-----------------
Given a linked list of N nodes. The task is to reverse this list.
Example 1:
---------
Input:
LinkedList: 1->2->3->4->5->6
Output: 6 5 4 3 2 1
Explanation: After reversing the list, elements are 6->5->4->3->2->1.
Example 2:
---------
Input:
LinkedList: 2->7->8->9->10
Output: 10 9 8 7 2
Explanation: After reversing the list,
elements are 10->9->8->7->2.
Your Task: The task is to complete the function reverseList() with head reference as the only argument and should return new head after reversing the list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
*/
// Link --> https://practice.geeksforgeeks.org/problems/reverse-a-linked-list/1#
// Code:
struct Node
{
int data;
struct Node *next;
}
class Solution
{
public:
//Function to reverse a linked list.
struct Node* reverseList(struct Node *head)
{
struct Node *prev , *cur , *next;
cur = head;
prev = next = NULL;
while (cur != NULL)
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head = prev;
return head;
}
};