Friday, July 18, 2008

Given a pointer to a node in a single linked list how do you delete it?

Technique:
Copy the contents of next node into the current node and then do the following things:
1) copy the next pointer into a temporary pointer
2) set current node's next to next node's next
3) delete the node pointed by temporary pointer.

Wednesday, July 9, 2008

Obtain the pointer to Nth last node in a single linked list.

Technique: Suppose you are given 2 buses Bus-A and Bus-B travelling always at 100mph and you have to maintain a distance of 20mps between them. Then you will achieve this by staring Bus-A first from the source station. You start Bus-B only when Bus-A is 20 miles from source station.

Given a string"My name is Girish" write a code snippet to make it "Girish is name My" (one of the optimistic ways)

This is a 2 step process:
Step 1 : Reverse the whole string "My name is Girish". It becomes "hsiriG si eman My".
Step 2: Reverse each word which makes it "Girish is name My".

Complexity: SIMPLE.