Hi! In this Data structures tutorial we are going to find solution of trending interview question how to reverse Singly List in java(keeping in mind time complexity should be O(n).
It may sound bit trickier to reverse Singly List, but it can be easily achieved by using 3pointers (previousNode, currentNode and nextNode)

What is Complexity of reversing Singly Linked List in java?
Complexity of reversing Singly SinglyList is O(n) in java.

Important methods used for reversing Singly Linked List in java >

Logic explanation of reversing Singly Linked List in java with diagram >

Initially we will keep 3 pointers:
previousNode pointing to null.
currentNode and nextNode pointing to first.

STEP1:
execute below while loop.

while(nextNode!=null){
nextNode=nextNode.next; //make nextNode point to next node.
currentNode.next=previousNode; //make current node's next point to previous node.
previousNode=currentNode;  //make previousNode point to currentNode.
currentNode=nextNode;   //make currentNode point to nextNode.
}

STEP2:
execute while loop again.
STEP3:
execute while loop again.
STEP4:
execute while loop again.
STEP5:
Execute below statement.
first=previousNode;  //which will make first point to previous node(i.e. last node).
Now we are reversed our LinkedList successfully. All the nodes are pointing to previous nodes.

Full Program/example/SourceCode for reversing Singly LinkedList>

So, in the above java program we wrote logic to reverse Singly linkedlist in java.

Complexity of reversing SinglyList in java >
Complexity of reversing SinglyList is O(n), because we got to iterate over each and every node to reverse the Singly LinkedList in java.

So, in this Data structures tutorial we found solution to trending interview question how to reverse Singly List in java(keeping in mind time complexity should be O(n).

Having any doubt? or you you liked the tutorial! Please comment in below section.

1) Stacks, Queues in Data Structures in java

2) Single LinkedList implementations in Data Structures in java:-

3) Doubly LinkedList implementations in Data Structures in java:-

5) Some of the tricky and interesting Single LinkedList implementations in Data Structures in java

6) Binary tree tutorial in java >