Q: All-time Most-Popular-Question: How to reverse a single link list? (Most companies ask!)
A: At least 2 popular/elegant ways to do this without using additional memory:
Non-Recursive & Recursive.
Tell me what’s the disadvantage of recursion? It’s damn expensive, be aware of stack overflow if the list is long.
// Non-Recursively (Most interviewers expect this sort of answer!)
Node *p = head; // h points to head of the linklist.
If( p == NULL)
return NULL;
link *h; //C++ style C programming
h = p;
if(p->next)
p = p->next;
h->next = NULL; // h is now an isolated node which will be the tail node eventually
while( p != NULL) {
link *t = p->next; //tmp node.
p->next = h;
h = p;
p = t;
}
return h; //h points to the new Head.
//-END-
// Recursively, In case you forgot.
reverse_ll(struct node ** hashref) {
struct node * first, last;
if(*hashref == NULL) return -1;
first = *hashref;
rest = first->next;
if(rest == NULL) return;
reverse_ll(&rest);
first->next->next = first; //reversion happens.
first->next = NULL;
} //-END-
Collateral Questions: Why recursion is evil?
A: Potential of Stack overflow for extensively nested recursions.
Q: How to print a link list reversely? (NetScreen, Netli, Yahoo!)
A: If Recursively:
rev_ll (Node *h) {
If(!h) return(-1) ;
else { rev_ll(h->next); print(h->data);}
}
If Non-Recursively:
Same as last Q/A: reverse the link-list then print out from the new head. Complexity: O(2*N)
//Similar question: How to reverse a string? Complexity O(N/2); exchange firstlast, 2nd2nd-to-the-last, etc…
Q: How to reverse doubly-linked list? (Akamai onsite San Mateo, CA)
A: // Basically, double linklist is easier to reverse, the only 2 things need to do are:
// tail head exchange; prev next exchange;
Node *PCurrent = pHead, *pTemp;
While(pCurrent) {
pTemp = pCurrent->next;
pCurrent->next = pCurrent->prev;
pCurrent->prev = pTemp;
pHead = pCurrent;
pCurrent = pTemp;
}
// pHead will point to the newly reversed head of the double link-list
// -END-
Q: How to insert a node into an ASCENDly sorted double link list? (Infoblox onsite)
A: // Note: the following code concerns all boundary conditions!!!
Assume we have a struct:
Struct Node {
Int data;
Node * prev;
Node *next;
};
//pHead, pCur, nn (new node), ppCur (pre-pCur)
if(pHead == NULL) return(0);
pCur = pHead;
while(pCur) {
if(pCur != pHead) {
ppCur = pCur->prev; //keep track of prev node.
}
if(pCur->data >= nn->data) {
if(pCur == pHead) { // insert at the head
pHead = nn;
nn->prev = NULL;
nn->next = pCur;
pCur->prev = nn;
} else { // insert at non head.
If(pCur->next == NULL) { // insert at the tail.
nn->next = NULL;
pCur->next = nn;
nn->prev = pCur;
} else { // insert somewhere in the middle.
nn->next = pCur;
pCur->prev = nn;
ppCur->next = nn;
nn->prev = ppCur;
}
return(pHead); // return head of double-linklist.
}
} else { // keep going!
pCur = pCur->next;
}
} // end of while()
Q: How to find a loop in a linked list (single link-list)/ Yahoo! Favorite onsite question.
A:
Node *p1, *p2, *head;
p1 = p2 = head;
do {
p1 = p1->next;
p2 = p2->next->next;
} while ( p1 != p2 && p1->next != NULL && p2->next != NULL && p2->next->next != NULL )
if ( p1->next == NULL || p2->next == NULL || p2->next->next == NULL) {
printf(“None loop found!n”;
} else if ( p1 == p2 && p1 != NULL ) {
printf(“Loop found!n”;
} // -END-
//Story Telling: A Yahoo! Director ever asked this question and he challenged me with this scenario: What if p1 and p2 fall into an endlessly looping hole? This imagined scenario/dilemma will NEVER happen as a simple reply! Don’t be afraid to stand off. This also propelled me to think what made one a Technical/Engineering Director? Stupidness, Arrogance or Ignorance? Maybe all.
Q: Print UNIQUE Array Element (PayPal onsite interview question) ????
A: // Assume it’s a char * array
char * unique_array(char *s, int size)
{
int i = 1, c = 1;
for(c=1; c<=size; ++c) {
if (s[c] != s[i-1]) { s[i] = s[c]; i++; }
}
s[i] = ‘