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

Показать описание
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:
Course Description Video :
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
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) {
}
}
public static void printNode() {
Node current = head;
while (current != null) {
}
}
public static Node reverseList(Node head) {
while(prev !=null) {
head = prev;
prev = next;
if(next !=null)
}
return head;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// creating and inserting node data
Node second = new Node(2);
Node third = new Node(3);
Node fourth = new Node(4);
Node fifth = new Node(5);
// Connection of Nodes
}
}
Hibernate Interview Questions and Answers:
Spring Boot Interview Questions and Answers:
Subscriber and Follow Code Decode
#linkedlist #datastructure #codedecode
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
Course Description Video :
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
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) {
}
}
public static void printNode() {
Node current = head;
while (current != null) {
}
}
public static Node reverseList(Node head) {
while(prev !=null) {
head = prev;
prev = next;
if(next !=null)
}
return head;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// creating and inserting node data
Node second = new Node(2);
Node third = new Node(3);
Node fourth = new Node(4);
Node fifth = new Node(5);
// Connection of Nodes
}
}
Hibernate Interview Questions and Answers:
Spring Boot Interview Questions and Answers:
Subscriber and Follow Code Decode
#linkedlist #datastructure #codedecode
Комментарии