Amazon Interview Question: Reverse linked list recursive... | Glassdoor.co.in

## Interview Question

Senior Software Engineer Interview Chennai

0

forward_list = {
'value': 'a',
'next': {
'value': 'b',
'next': {
'value': 'c',
'next': None
}
}
}

def reverse_list(node):
'''
Reverses a linked list. Returns the supplied node and the new head of the
list.
'''
# The new head of the list.

# If we're not at the end, reverse the next nodes and add this node to the
# chain.
if node['next']:
result['next'] = node
node['next'] = None

# If we're at the end, this statement is the base case; return the newly
# reversed list.

def print_list(node):
print node['value'],
if node['next']:
print_list(node['next'])

# Test code.
print 'Forward list:',
print_list(forward_list)

print

# Print the reverse list.
print 'Reverse list:',

llvllatrix on 16-Jun-2012
1

Node * reverse( Node * ptr , Node * previous) {
Node * temp;
if(ptr-&gt;next == NULL) {
ptr-&gt;next = previous;
return ptr;
} else {
temp = reverse(ptr-&gt;next, ptr);
ptr-&gt;next = previous;
return temp;
}
}

devsathish on 17-Aug-2012
0

/*
Jun Zheng, Rice Univ
C++, Xcode 4.5.2
Real interview question of Amazon &amp; Schlumberger
*/
node* p2=p1-&gt;next;
}

//Auxiliary method for void reverseRec(node* &amp;phead)
node* reverseRecHelper(node* &amp;p1, node* &amp;p2){
if(p2==NULL) return p1;
node* p3=p2-&gt;next;
p2-&gt;next=p1;
p1=reverseRecHelper(p2, p3);
return p1;
}

Jun Zheng on 04-Mar-2013