How to Reverse a LinkedList in Java | Data Structure and algorithm | Leetcode 206 | Code Decode

Опубликовано: 29 Сентябрь 2024
на канале: Code Decode
14,025
201

In this video of code decode we have explained leetcode 206 i.e. reverse a linked list which is most commonly asked linked list interview question.
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
https://openinapp.co/udemycourse

Course Description Video :
https://yt.openinapp.co/dmjvd

PlayStore Link http://nxtlvl.in/hlc
AppStore Link http://nxtlvl.in/fhg

Given the head of a singly linked list, reverse the list, and return the reversed list.


Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

In this video of how to reverse a linked list

We have taken three pointers head , prev and next and kept at respective positions

Now in each iterations our prev.next will point to head and we will move head , prev , next respective to next position and iteration will continue.

At the end our head will point to the last element of the original linked list and all pointers will be reversed. Thereby printing the whole original linked list
in the reverse direction.
Also the pointers taken i.e. prev and next will point to null and will garbage collected.
Thus the code of Reverse Linked list is having a time complexity of O(n)


public class LinkedList {

static Node head;

private static class Node{
private int data;
private Node next;

public Node(int data) {
this.data = data;
this.next = null;
}
}

public static void printNode() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -- ");
current = current.next;
}
System.out.println("null");
}

public static Node reverseList(Node head) {
Node prev = head.next;
Node next = head.next.next;
head.next = null;

while(prev !=null) {
prev.next=head;
head = prev;
prev = next;
if(next !=null)
next = next.next;
}
return head;
}



public static void main(String[] args) {
LinkedList list = new LinkedList();

// creating and inserting node data
LinkedList.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
Node fourth = new Node(4);
Node fifth = new Node(5);

// Connection of Nodes
LinkedList.head.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;

LinkedList.printNode();
LinkedList.head = LinkedList.reverseList(LinkedList.head);
LinkedList.printNode();
}



}


Most Asked Core Java Interview Questions and Answers:    • Core Java frequently asked Interview ...  
Advance Java Interview Questions and Answers:    • Advance Java Interview Questions  

Java 8 Interview Questions and Answers:    • Java 8 Interview Questions(New Features)  

Hibernate Interview Questions and Answers:
   • Hibernate Interview Questions Java  

Spring Boot Interview Questions and Answers:
   • Advance Java Interview Questions  

Angular Playlist:    • Angular Course Introduction || Angular 8  
SQL Playlist:    • SQL Interview Questions and Answers  

GIT:    • GIT  

Subscriber and Follow Code Decode
Subscriber Code Decode: https://www.youtube.com/c/CodeDecode?...
LinkedIn :   / codedecodeyoutube  
Instagram:   / codedecode25  

#linkedlist #datastructure #codedecode