Google Interview Question

2- Reverse linked list (first phone interview)

Interview Answers

Anonymous

Apr 6, 2015

struct ListNode { int val; ListNode *next; ListNode (int v):val(v),next(NULL){} }; ListNode *reverseList(ListNode *head) { if(NULL==head || NULL==head->next) return head; ListNode *cur=head,*pre=NULL; while(cur) { ListNode *tmp=cur->next; cur->next=pre; pre=cur; cur=tmp; } return pre; }

Anonymous

Mar 4, 2015

I did well, solving it recursively in a few minutes. Then I was asked to solve it iteratively.