L6. Odd Even Linked List | Multiple Approaches

preview_player
Показать описание


Please do give us a like, and subscribe to us if you are new to our channel.

Рекомендации по теме
Комментарии
Автор

Great video just a small correction that it will be
even = even.next
Here's the full code for leetcode:
ListNode* oddEvenList(ListNode* head) {
if(head == NULL || head->next == NULL) return head;

ListNode* odd = head;
ListNode* even = head->next;
ListNode* evenHead = head->next;

while(even!=NULL && even->next!=NULL){
odd->next = odd->next->next;
even->next = even->next->next;

odd = odd->next;
even = even->next;
}

odd->next = evenHead;
return head;
}

edisane
Автор

if anyone is facing any issue with the while condition ie,
while(even != NULL && even -> next != NULL)
you can use instead,
while(odd -> next != NULL && even -> next != NULL)
i hope it helps you, happy coding.

mdtanveeransari
Автор

one mistake
even = even->next instead of even=even->next->next

CodeNinja
Автор

well explained!
There is slight error in the brute force code that in while loop at the last line in loop even=even.next; so if anyone find its confusing can be helped from this.

champ-kxlb
Автор

hey guys, in the optimal solution, inside while loop, we have already set the next for both even and odd, so to go next even and odd use even= even->next ; odd= odd->next respectively
I spend my 20-30 minutes realising this lol :)

NotNewton
Автор

O(n/2) space complexity.

i was wrong. it'll be O(n) because we need to consider the operations taking place twice inside the array

pushankarmakar
Автор

i think even next should also be even.next rather than even.next.next

lovetiwadiai
Автор

Why is the time complexity O(n/2) * 2? It should be O(n/2) regardless of the number of operations performed inside the loop, right?

chetandatta
Автор

We would need to check if head !=null before initializing even as head.next and while updating even, even.next should be good enough, even.next.next would land us on an odd node.

shashamnk
Автор

this is the java code
*
*
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head==null ||head.next==null){
return head;
}
ListNode odd=head;
ListNode even=head.next;
ListNode connector=head.next;
while(even!=null && even.next!=null){
odd.next=odd.next.next;
even.next=even.next.next;

odd=odd.next;
even=even.next;
}
odd.next=connector;
return head;
}

bhashasinha
Автор

All the video lectures and the articles helped me a lot to gain confidence in DSA and will be helping me in the interviews. Thank you Striver bhaiya for bringing such amazing content for free.

AmanSharma-xyqm
Автор

line even = even.next.next; inside the while loop. When updating the even pointer, you should check if even.next is not null before trying to access even.next.next. Small correction to be made

ketonesgaming
Автор

Here is the java program
class Solution {
public ListNode oddEvenList(ListNode head) {
//edge case
if(head==null || head.next==null) return head;
ListNode odd=head;
ListNode even=head.next;
ListNode evenHead=head.next;
while(even !=null && even.next!=null){
odd.next=odd.next.next;
even.next=even.next.next;
odd=odd.next;
even=even.next;
}
odd.next=evenHead;
return head;

}
}

rajmohitkumar
Автор

LC 328.

// Naive solution
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
// naive solution is using a list to store the data

vector<int> arr;
ListNode* temp = head;

if(head ==NULL || head->next == NULL ) return head;
while(temp!=NULL && temp->next!=NULL){
arr.push_back(temp->val);
temp = temp->next->next;
}
if(temp) arr.push_back(temp->val);

temp = head->next;
while(temp!=NULL && temp->next!=NULL){
arr.push_back(temp->val);
temp = temp->next->next;
}
if(temp) arr.push_back(temp->val);

int i =0; temp = head;
while(temp!=NULL){
temp->val = arr[i];
i++;
temp = temp->next;
}

return head;

}
};

// optimised solution

class Solution {
public:

ListNode* oddEvenList(ListNode* head) {


if(head == NULL || head->next == NULL ) return head;
ListNode* oddptr = head;
ListNode* evenptr = head->next;
ListNode* evenhead = head->next;

while(evenptr !=NULL && evenptr->next!=NULL){
oddptr->next = oddptr->next->next;
evenptr->next = evenptr->next->next;

oddptr = oddptr->next;
evenptr= evenptr->next;
}

oddptr->next = evenhead;

return head;
}
};

abhinavprabhat
Автор

Marking my Day 21 of learning DSA. Thanks for the great course with clear in-depth explanation

manishmahajan
Автор

solved without watching, kudos to previous videos base creation !!

BhavyaJain-qzjg
Автор

Why did the brute force seemed more complex than the optimized 💀

Sawlf
Автор

we need the article for rest of the linked list problems of A2Z sheet

mdtanveeransari
Автор

i can surely say that this is the best linkedList course of all time

amangoyal
Автор

0(n)time complexity, understood thank you striver bhaiya

nitishkumarram