-
Notifications
You must be signed in to change notification settings - Fork 0
/
p165.java
78 lines (67 loc) · 1.79 KB
/
p165.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*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).
Constraints:
1 <= N <= 104*/
/* linked list node class:
class Node {
int data;
Node next;
Node(int value) {
this.value = value;
}
}*/
class Solution{
// Extra Space
Node reverseList(Node head)
{
List<Integer> ls=new ArrayList<>();
Node curr=head;
while(curr!=null){
ls.add(curr.data);
curr=curr.next;
}
Node temp=head;
for(int i=ls.size()-1;i>=0;i--){
temp.data=ls.get(i);
temp=temp.next;
}
return head;
}
// Without extra space optimized way
/* Approach
Initialize two pointers prev as NULL, curr as head.
Iterate through the linked list. In a loop, do the following:
Before changing the next of curr, initialize temp node and store the next node
temp = curr -> next
Now update the next pointer of curr to the prev
curr -> next = prev
Update prev as curr and curr as next
prev = curr
curr = temp */
Node reverseList(Node head){
Node curr=head;
Node prev=null;
while(curr!=null){
Node temp=curr.next;
curr.next=prev;
prev=curr;
curr=temp;
}
return prev;
}
}