Skip to content
Prev Previous commit
Next Next commit
delets unnecessary file from the folder
  • Loading branch information
micomike committed May 6, 2022
commit 53b8079f2c81108618feb1dc8d8571eb0b2db71b
25 changes: 25 additions & 0 deletions 02. Algorithms/10. LinkedList/Reverse LinkedList (Easy)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Leetcode Link: https://leetcode.com/problems/reverse-linked-list/

To reverse a LinkedList, we need to reverse one node at a time.
We will start with a variable current which will initially point to the head of the LinkedList
and a variable previous which will point to the previous node that we have processed;
initially previous will point to null.In a stepwise manner, we will reverse the current node by pointing
it to the previous before moving on to the next node
Also, we will update the previous to always point to the previous node that we have processed.

Here is visual representation of how previous is populated in each iteration
Example: 4(head) -> 3 -> 0 -> 9
1: previous = 4 -> null
2: previous = 3 -> 4
3: previous = 0 -> 3
4: previous = 9 -> 0

By the end of the iteration 9 is point to 0(9->0). if we go to 0's next field we get 3 and so on until null

Time complexity
The time complexity of this algorithm will be O(N) where ‘N’ is the total number of nodes in the LinkedList.

Space complexity
I only used constant space, therefore, the space complexity of this algorithm is O(1)


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

/** Refer to the README to know how the algorithm works and to find the link for the question.
* @param node the head of the linked list
* @return the reversed linked list
*/
public static ListNode reverseList(ListNode node) {
ListNode current = node; // stores the current node in the linked list
ListNode previous = null; // previous node that is processed
ListNode nextNode = null ; // a temporary node that will store the next node

while (current != null) {
nextNode = current.next; // temporarily stores the next node
current.next = previous; // reverse the current node
previous = current; // point previous to the current node
current = nextNode; // move to the next node
}

return previous; // returns the previous node which has the reversed list
}


This file was deleted.

This file was deleted.

Empty file.