Find Middle of a singly linked list(Algorithm/Code/Program)

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

Given a linked list. Find the middle of the singly linked list.
Рекомендации по теме
Комментарии
Автор

Awesomely simple! Best and clear-cut, to-the-point explanation, just the way it should be! Thanks a ton :)

vikassanwal
Автор

You make it look so easy! Good work Mr Vivekanand! Keep it up

gurjotahuja
Автор

I learned a lot from ur channel...awesome explanation... big big thanks

ajayaman
Автор

Splitting linked list in the two pieces If the condition for while loop is false at the begining then first piece is empty, second has at most head element (if list is empty both pieces are empty)
If while condition is fulfilled at least once we should break the list just before node you found
For splitting node we have to consider borded cases - at most one node on the list
If we have more than one node we need node previous to that you found
If the list has more then one element you are looking the begining of the second piece
but more useful would be end of the first piece
Your algorithm could be accepted for splitting linked list if it gives c node as a result in both your
examples then for border cases head as midpoint will be ok

holyshit
Автор

Thanks for the help men! Greatly appreciated!

Lucho
Автор

Amazing explanation, thank you good Sir

ViiCiiOUZ
Автор

Sir can plz explain how to write module & procedure in data structure.
e. g. An array A contains 25 positive integers. Write a module to find all the pair of number whose sum is 25.

neetusharma
Автор

Thank you so much for the nice explanation

AshishBurnwal
Автор

For even number of elements, and get the output as 'c'. Can you give the code ..

naginenikarthik
Автор

nice sir... but u don't handle other cases like if linked is empty, if there is only 1/2 element in linked list...

wasit-shafi
Автор

will this work for more than 5 or 6 elements?

avssridhar
Автор

Sir what if we have only 2 elements in linked list in that case the output should be 1st element (2/2=1) but according to your approach o/p will be 2nd element.

ashishbasantani
Автор

please post alg/code for number of duplicates of a number in a linked list

muskanrath
Автор

please upload a tutorial of tower of Hanoi....without

ajayaman
Автор

sir please upload singly, doubly, circular link list all topics

shivasureshreddysiva
Автор

plz brother make videos on selection, bubble sort by changing

ashishsinha
Автор

In doubly linked list we could traverse from both directions from head to tail using next pointers
and from tail to head using prev pointers
For singly linked list we can use two pointers, first goes once per iteration and second goes twice per iteration
For merge sort purpose c node should be tail node of first sublists in both examples

Lets consider modification to your algorithm

node * find_middle(node * head)
{
node *p, *q;
p = head;
q = head;
while(q != NULL && q->next !=NULL)
{
q = q->next->next;
if(q !=NULL)
p = p->next;
}
return p;
}
Now we can split linked list into halves which is necessary for merge sort

holyshit
Автор

merge two sorted linked list plz upload this video also

rishabhsingh-gwnm
Автор

If we want to use this function as subroutine for merge sort this function needs correction
I see two ways to correct this function

// Left sublist longer convention

node * find_middle(node *head)
{
node *p, *q;
p = head;
q = head;
while(q != NULL && q->next != NULL)
{
q = q->next->next;
if(q != NULL)
p = p->next;
// I changed order of instructions and added if statement
}
return p;
}

// Right sublist longer

node * find_middle(node *head)
{
node *p, *q, *r;
p = head;
q = head;
r = NULL;
// I introduced pointer to previous node of his middle node
while(q != NULL && q->next != NULL)
{
r = p;
p = p->next;
q = q->next->next
}
return r;
}

holyshit
Автор

Awesomely simple! Best and clear-cut, to-the-point explanation, just the way it should be! Thanks a ton :)

jainuinelyselfish