Facebook Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
4
of 1 vote

void reverseLinklist(Node **head)
{
Node *ahead=NULL, *curr=NULL, *behind=NULL;
behind=NULL;
curr=*head;
if(curr)
{
ahead=curr->next;

while(ahead)
{
curr->next=behind;
behind = curr;
curr = ahead;
ahead=ahead->next;
}

curr->next=behind;
*head=curr;
}
}

- Tushar November 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 vote

node *reverseList(node *head)
{
node *p1, *p2, *p3;

if(head == NULL || head->next == NULL)
return head;

p1=head; p2=head->next;
while (p2)
(
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
)

head -> next = NULL;
head = p1;
return head;
}

- Jinai October 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 vote

void reverseList(node **head)
{
node *result=NULL;
while((*head) != NULL)
{
node *tmp=(*head)->next;
*head->next=result;
result=*head;
*head=tmp;
}
head=*result;
}

- Abhi November 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 vote

main()
{
reverse(head, NULL);
}

void reverse(node* current, node* previous)
{
if(current->next != NULL)
reverse(current->next, current);
current->next = previous;
}

- Vikram February 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This has everything except updating the head ptr, which would still be pointing to the original head (now at the end of the list).

- newt January 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node reverseList(node head) {


node p1, p2;

p1 = p2 = head;

p2 = p2->next;
p1->next = NULL;

while( p2 != null) {


node temp = p2; p2= p2 ->next;

temp->next = p1;
p1 = temp;

}

return p1;
}

- Neo November 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I always right it the following way, I think it is more legible

void reverseLinklist(Node **old_list) {
node* new_list = null;
node* element = null;
while (*old_list) {
//Extract top node from old_list
element = *old_list;
*old_list = old_list->next;

//Add to front of new list;
element->next = new_list;
new_list = element;
}
//set old_list to point to correct node (new_list), or return
//new_list depending on function signature
*old_list = new_list;
}

- Anonymous November 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct MyList MyList;
struct MyList {
int id;
MyList * next;
};

MyList * reverse_list(MyList * list) {

MyList * curNode = NULL, * nextNode = NULL, * tmp = NULL;

curNode = NULL;
nextNode = list;
while(nextNode != NULL) {
tmp = nextNode->next;
nextNode->next = curNode;
curNode = nextNode;
nextNode = tmp;
}

return curNode;
}

- freeaion March 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wow! Everyone knows this one.........

- Anonymous May 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to do it recursively......

- abc January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi,how to do it recursively.......

- raghavendra96 January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution. Let me know if there is something better :

node* reversed_pointer = reverse(head, NULL);

node* reverse(node* curr, node* prev){
  node* temp = curr->next;
  curr->next = prev;
  if(temp == NULL)
    return curr;
  else
    return reverse(temp, curr);
}

- milwac March 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The if-else can be removed.

node* reversed_pointer = reverse(head, NULL);


node* reverse(node* curr, node* prev){
  if (curr == NULL) return prev;
  node* temp = curr->next;
  curr->next = prev;    
  return reverse(temp, curr);
}

- ArtfulCoder August 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

reverse(NODE *ptr){
NODE *tmp=head;
while(!head->next){
head=head->next;
reverse(head);
tmp->next->next=tmp;
tmp->next=NULL;
}
}

- Anonymous May 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Node* Reverse(Node *head)
{
   if (head == NULL) return head;
   if (head->Next == NULL) return head;
   else
   {
       Node * adjacentNode = head->Next;
       head->Next = NULL;
       Node * newHead = Reverse(adjacentNode );
       adjacentNode ->Next = head;
       return newHead;
   }

}

- ArtfulCoder August 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node prev = null;
Node cur = root;
while(cur != null)
{
	Node next = cur.Next;
	cur.Next = prev;
	prev = cur;
	cur = next;
}

// prev is new root of the list

- anvol April 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Code

public static void main(String[] args){
		SLinkedList sl=new SLinkedList();
		sl.insertFirst(1);
		sl.insertFirst(2);
		sl.insertFirst(3);
		sl.insertBefore(4, 2);
		sl.insertAfter(5,4);
		/*sl.reverse();*/
		sl.recursiveReverse(sl.root);		
		
		sl.display();
	}
}

	class Node{
	
		int data;
		Node next;
		public Node(int d){
			this.data=d;
		}
}
	class SLinkedList{
		Node root;
		public SLinkedList(){
			
						
			
		}
		
		
		public void reverse(){
			if(root==null || root.next==null)
				return;			
			else if(root!=null && root.next!=null && root.next.next==null){
				Node curr=root;
				Node next=root.next;
				next.next=curr;
				root=next;
			}
			else{
				Node curr=root;
				Node next=curr.next;
				Node prev=null;
				
				while(curr.next!=null){
					
					next=curr.next;					
					curr.next=prev;
					
					prev=curr;
					curr=next;					
				}
					
				curr.next=prev;
				root=curr;
			}
		}
		
		
		public void recursiveReverse(Node n){
				Node curr=n;
				if(curr==null)
					return;
				
				if(curr.next==null){
					root=curr;
					return;
				}
					
				
				recursiveReverse(n.next);
				
				curr.next.next=curr;
				curr.next=null;
				
								

		}
		
		public void insertFirst(int d){
			Node n=new Node(d);
			if(root==null)
				root=n;
			else{
				n.next=root;
				root=n;
			}
				
		}
		
		public void insertBefore(int v,int d){
			if(root==null)
				return;
			else{
				Node n= new Node(v);
				Node curr=root;
				Node prev=null;
				while(curr.data!=d){
					prev=curr;
					curr=curr.next;
					
					if(curr==null)
						return;
				}
				prev.next=n;
				n.next=curr;
			}
		}
		
		public void insertAfter(int v,int d){
			if(root==null)
				return;
			else{
				Node n=new Node(v);
				Node curr=root;
				Node next=null;
				while(curr.data!=d){
					curr=curr.next;
					next=curr.next;
					
					if(curr==null)
						return;
				}
				
				
				curr.next=n;
				n.next=next;
								
			}
		}
		
		public void remove(int d){
			if(root==null)
				return;
			else{
				Node curr=root;
				Node prev=null;
				while(curr.data!=d){
					prev=curr;
					curr=curr.next;
					
					if(curr==null)
						return;
				}
				
				prev.next=curr.next;
				curr=null;							
				
			}
		}
		
		
		
		
		
		public void display(){
			if(root==null)
				return;
			else{
				Node curr=root;
				while(curr!=null){
					System.out.print(curr.data + " ---> ");
					curr=curr.next;
				}
			}
		}

- Youngsam September 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ListNode* reverse(ListNode *head) {
	ListNode* current, *next, *prev;
	prev=NULL;
	current=head;
	while(current) {
		next=current->next;
		current->next=prev;
		prev=current;
		current=next;
	}
	return prev;
}

- somnathrs25 September 02, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More