Linked list in Java - 72: Rearrange a linked list in zig-zag manner

  Рет қаралды 4,461

Coding Simplified

Coding Simplified

Күн бұрын

Source Code: thecodingsimpl...
Solution:
Take a boolean variable flag
Now, iterate from 1st node of list & check if element is less than next element
Same way, in next iteration check if it's greater than next element
In both condition, if condition matches, swap the values
At last, we'll have arranged value
Time Complexity: O(n) for iterating the n elements of linked list
Space Complexity: O(1) as we're taking only constant variables
Please check video for more info:
This problem is similar to:
how to do rearrange a linked list in zig zag manner,
arrange a linked list in zig zag manner,
linked list in zig zag manner,
linked list,
zig zag,
zig-zag,
arrange,
tutorial,
coding simplified,
java
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
I started my KZbin channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 300+ videos. My account is Partner Verified.
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
★☆★ SUBSCRIBE TO ME ON KZbin: ★☆★
www.youtube.co...
★☆★ SEND EMAIL At: ★☆★
Email: thecodingsimplified@gmail.com

Пікірлер: 4
@ayeshaadhikari6123
@ayeshaadhikari6123 3 жыл бұрын
thank you sir! only your explanation was enough :)
@CodingSimplified
@CodingSimplified 3 жыл бұрын
Thanks for your nice feedback. Keep Watching.
@prasannapm3220
@prasannapm3220 2 жыл бұрын
thank you sir
@rdxgaurav3483
@rdxgaurav3483 2 жыл бұрын
Node *zigZag(Node* head) { // your code goes here if(head == NULL || head->next == NULL){ return head; } Node* node = head; bool flag=true; while(node != NULL && node->next != NULL){ // if I am running without this node->next != NULL its giving segmentation fault , I don't know how the code in video is working but if we are at last node then the NULL->data will not be there and it will give runtime seg. error if(flag){ if(node->data > node->next->data){ swap(node->data, node->next->data); } flag = !flag; } else{ if(node->data < node->next->data){ swap(node->data , node->next->data); } flag = !flag; } node = node->next; } return head; }
Linked List in Java - 73: Flatten a multi level Linked list level wise
10:40
The selfish The Joker was taught a lesson by Officer Rabbit. #funny #supersiblings
00:12
How Strong is Tin Foil? 💪
00:26
Preston
Рет қаралды 147 МЛН
Convert array into Zig-Zag fashion | GeeksforGeeks
8:53
GeeksforGeeks
Рет қаралды 43 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 242 М.
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 411 М.
5 Signs of an Inexperienced Self-Taught Developer (and how to fix)
8:40
you will never ask about pointers again after watching this video
8:03
How to Use the Two Pointer Technique
10:56
Team AlgoDaily
Рет қаралды 104 М.
Check for Palindromic Linked List | Snapdeal | Adobe | Amazon
13:40
take U forward
Рет қаралды 133 М.