Check if singly linked list is Palindrome

There are couple of solution to it : Method 1 - Using the stack A simple solution is to use a stack of list nodes. This mainly involves three steps. Traverse the given list from head to tail and push every visited node to stack. Traverse the list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node. If all nodes matched, then return true, else false. [Read More]

Middle of a linked list

Problem Find the middle node of the linked list. Follow up - How will you get it in first pass. Solution Method 1 - Get the length and travel to middle Traverse the whole linked list and count the no. of nodes. Now traverse the list again till count/2 and return the node at count/2. Method 2 - Uses one slow pointer and one fast pointer Traverse linked list using two pointers. [Read More]

Detecting a loop in a linked list

Problem : Given a singly linked list, find if there exist a loop. Example A -> B -> C -> D -> E -> C [ E again point to C] Solutions Method 1 - Using hashing Traverse the list one by one and keep putting the node addresses in a Hash Table. At any point, if NULL is reached then return false and if next of current node points to any of the previously stored nodes in Hash then return true. [Read More]