Тёмный

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

Code Decode
Подписаться 136 тыс.
Просмотров 14 тыс.
50% 1

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:
openinapp.co/u...
Course Description Video :
yt.openinapp.c...
PlayStore Link nxtlvl.in/hlc
AppStore Link 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 a...
Advance Java Interview Questions and Answers: • Advance Java Interview...
Java 8 Interview Questions and Answers: • Java 8 Interview Quest...
Hibernate Interview Questions and Answers:
• Hibernate Interview Qu...
Spring Boot Interview Questions and Answers:
• Advance Java Interview...
Angular Playlist: • Angular Course Introdu...
SQL Playlist: • SQL Interview Question...
GIT: • GIT
Subscriber and Follow Code Decode
Subscriber Code Decode: www.youtube.co...
LinkedIn : / codedecodeyoutube
Instagram: / codedecode25
#linkedlist #datastructure #codedecode

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 16   
@nehamittal1935
@nehamittal1935 Год назад
Great but this doesn't cover the corner cases if the linked list contains just one node or a NULL is passed.
@nikhil5304
@nikhil5304 Год назад
You deserve more subscribers and views
@CodeDecode
@CodeDecode Год назад
Thanks for the nice words
@kevalmjain2872
@kevalmjain2872 Год назад
thanks so much, this was so much better, simple and straight to point compare to other videos, i also understood the linkedlist data structure....get this woman more subs!
@CodeDecode
@CodeDecode Год назад
Means a lot to us Keval 🙏🙏❤❤❤❤❤
@arpitsik4649
@arpitsik4649 Год назад
Present Mam❤️💯
@CodeDecode
@CodeDecode Год назад
🙂🙂
@mdpinnu
@mdpinnu Год назад
can you please add the link in the description to previous video
@nikhil5304
@nikhil5304 Год назад
Thank you mam
@CodeDecode
@CodeDecode Год назад
you're welcome
@akashsaha9366
@akashsaha9366 Год назад
Great maam👍
@CodeDecode
@CodeDecode Год назад
🙂🙂👍👍
@rahulshukla3696
@rahulshukla3696 Год назад
superb explained , well done . keep it up !!
@CodeDecode
@CodeDecode Год назад
Thanks Rahul 🙂🙂
@santosh0513
@santosh0513 Год назад
Simply superb, thank you ☺️
@CodeDecode
@CodeDecode Год назад
you're welcome santosh
Далее
ТАРАКАН
00:38
Просмотров 1,6 млн
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Просмотров 236 тыс.
8 patterns to solve 80% Leetcode problems
7:30
Просмотров 377 тыс.