Skip to main content

Posts

Showing posts with the label Reverse SLL

Find middle node of a linked list

How do you find the middle node of singly linked list of unspecified length, by traversing the list only once? Solution: Take two pointers p1 and p2.                Let p1 and p2 point to head p1=p2 = head; Advance p1 by one node at a time and p2  by 2 nodes at a time                            p1 = p1->next;                           p2 = p2 ->next->next;               When p2 reaches NULL, p1 will be in the middle node. Here is the code for the same NODEPTR find_mid_node (NODEPTR head) { NODEPTR t1 ,t2; t1 = t2 = head; while (t2 != NULL && t2 -> ne...