Want a Free Job Posting?

Buy a job posting today and the second one is on us. For a limited time only. Act Now.

Interview Question

Interview Seattle, WA

Reverse a linked list - both iterative and recursive way.


Interview Answer

2 Answers


Node reverse(Node head) { Node temp; Node previous = NULL; while (head != NULL) { temp = head.next; head.next = previous; previous = head; head = temp; } return previous; }

Anon on Dec 4, 2013

void reverse(Node* & curr) { if(!curr) return; Node* p=curr->next; if(!p) return; reverse(p); curr->next->next=curr; curr->next=NULL; curr=p; }

anonymous on Jan 9, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.