Reverse a Linked List Code/Algorithm/Program

preview_player
Показать описание
Given a Singly Linked List. Reverse the Linked list.
Рекомендации по теме
Комментарии
Автор

Superb explanation Sir.
Here's another approach of reversing a linkedlist recursively-
private static LinkedListNode start, LinkedListNode prev) {
if (start == null || start.next == null) {
start.next = prev;
return start;
}
LinkedListNode second = start.next;
start.next = prev;
return reverseLinkedList(second, start);
}

abhishekrajanofficial
Автор

Amazing, worth watching, thank you sir, khup madat Parat ekda Banvat rha videos...

aadeshn
Автор

Your visualization thing is very helpful. Thank you.

sarthaksrivastav
Автор

Sir interview me web project ke liye kis type ke project se kaam chl sakta h or jada problem na ho.
1. Complete mern stack project.
2. Any simple react project
3. Simple With html, css or javascript.
4. Any dsa project
5. Or any dbms.

Inme se konse rakha acha rhega or sirf kitno se kaam ho jayega pls reply.

wizardfire
Автор

but if we return q after breaking at if(q==NULL) then how we can get head of reversed list?

abinvarkey
Автор

We can simplify his code a little bit if we assume that we have lazy boolean evaluation
node *reverse(node *head)
{
node *p, *q;
if(head == NULL || head->next == NULL)
return head;
p = head;
q = p->next;
q = reverse(q);
p->next->next = p;
p->next = NULL;
return q;
}

holyshit
Автор

From this code it seems, "p->next->next = p" is getting executed only once. But it should be executed number of times recursive function is executed. Could you please explain this?

priyankamorankar
Автор

hats off to you sir..you deserve million subscribers.

aakashgoswami
Автор

Please Sir can we change this instruction :
P->next->next =p
By
q->next = p

miasevda
Автор

Sir....all your videos are so good...you write code first on board and explain in a beautiful way....thank you...Dr.Padmaja.Pulicherla

dr.padmajapulicherla
Автор

Can you please make solution video for Divide Two Integers. Thank you.

sauru
Автор

It is time to iterative approach now
Solution based on stack

node * reverse(node *head)
{
node *p, *q, *r;
p = NULL; // we intialize pointer to reversed list
q = head; // we initialize traversal pointer
while(q != NULL)
{
r = q->next // we save next pointer in list with original order of elements
q->next = p;
p = q; // In these two lines we perform push operation known from stack
q = r; // we get pointer to the next element
}
return p;
}

holyshit
Автор

sir u have described the function which returns a node, then how can u simply write return without mentioning any pointer along with it.At last how will we able to know the address of new head??

mihirsheth
Автор

need to use a tail variable for deleting the last node from a link list :( please help

yogifarm
Автор

is this algorithm valid in c langage ???

hajarchaibi
Автор

This code will not give correct output in computer. For correction We need to return head Pointer from base case then it will work.

NeverGiveUp_oo
Автор

could you please make one or two videos regarding working of recursions, i.e. like generating binary strings for given no.s etc

ashokbhandari
Автор

I don't understand efficiently please sir how i identify the head and null or execute

beleynewwubagegn
Автор

May I know, Why have you stopped posting videos?

vivekhattarge
Автор

Thanks a lot. Really you have the teaching capability. Any one can understand.

manasranjan