Linked List Problems in Java - 40 : Swap nodes of given values without swapping data
HTML-код
- Опубликовано: 10 окт 2024
- Source Code: thecodingsimpl...
In this video, we're going to reveal exact steps to Swap nodes of given values without swapping data in linked list in Java
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
I started my RUclips channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 200+ videos.
★☆★ SUBSCRIBE TO ME ON RUclips: ★☆★
www.youtube.co...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
This Problem is synonym of following problems:
Swap nodes of given values without swapping data in linked list in Java,
Swap nodes of given values without swapping data in linked list,
how to Swap nodes of given values without swapping data in linked list,
swap nodes in linked list,
coding simplified,
linked list,
java,
So for when they are adjacent to each other code goes like this ?
/* code where you check if the prev is null or not */
if(prev1 == t2) {
t2.next = t1.next;
t1.next = t2;
return head;
} else if(prev2 == t1) {
t1.next = t2.next;
t2.next = t1.next;
return head;
}
/* code with tmp */
I don't think so it will work if nodes to be swapped are adjacent nodes.
You have missed out a case where the elements are adjacent to each other.
Thanks for answering in your other comment. I'll check it further.
@@CodingSimplified The Code is correct (I have run a simlar code on gfg), this is handling the adjacent cases also.
@@helloUser12373 chup bilkul chup
//Code for adjacent nodes:
if(j == i+1){
LinkedListNode prev = null;
LinkedListNode c1 = null;
LinkedListNode c2 = null;
int x = 0;
int y = 1;
while(x
For anyone who needs help where the nodes are adjacent to each other, here is a small snippet:
ListNode firstNode = head;
ListNode secondNode= firstNode.next;
head=secondNode;
secondNode.next=firstNode;
firstNode.next=null;
return head;
The Code is correct (I have run a similar code on gfg), this is handling the adjacent cases also.
@@helloUser12373 chup bilkul chup