Interview Question


Country: United States




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

swap first two nodes and then call recursively for the rest of the linked list

void swapaltrec(list *head)
{
    list p=*head,q;
    if(!p || !(p->next))
    return;
    q=p->next;
    p->next=q->next;
    q->next=p;
    *head=q;
    swapaltrec(&(p->next));
}

- Amit August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

At last when you are passing the address of the node then the function call's syntax should be changed, It should be pointer to pointer to the head that is list **head.

- vgeek August 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually here list * means struct node **
when i wrote the prog i used following--

typedef struct node *list;
struct node{
int data;
list next;
};

do tell me,if u still feel,something is wrong

- Amit August 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swapNode(list *head)
{
	list *temp, *temp1;
	temp = head->next;
	while (head && temp)
	{
		temp1 = temp->next;
		head->next = temp1;
		temp->next = head;

		head = temp1->next;
		temp = head->next;
	}
}

- Varun August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

procedure(Node start){
	
	if(start == null) return;

        Node x = start;
        Node y = x.next;

	while(x!= null and y != null)
	{

		x.next = y.next;
		y.next = x;
		
		x = x.next;
		if(x == null) return;
		y = x.next;
	}
}

- anshulzunke August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This code still has issues since we are not modifying the start of the linked list which will get modified too in case of swapping 1st and 2nd node of the linked list

procedure(Node start){
	
	if(start == null) return;

	boolean startNodeModifed = false;
        Node x = start;
        Node y = x.next;

	while(x!= null and y != null)
	{
		
		
		x.next = y.next;
		y.next = x;
		if(!startNodeModifed) 		
			start = y;
			
		x = x.next;
		if(x == null) return;
		y = x.next;
	}
}

- anshulzunke August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swp(llist *head)
{
    llist *a,*b;
    if(head->next==NULL || head->next->next==NULL) return;
    a=head;
    b=head->next;

    int tmp=a->x;
    a->x=b->x;
    b->x=tmp;

    if(b->next!=NULL)
        swp(b->next);
    return;
}

- raihanruhin August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

full code here:

#include<iostream>
using namespace std;

struct llist{
    int x;
    llist *next;
    llist()
    {
        next=NULL;
    }
};

void swp(llist *head)
{
    llist *a,*b;
    if(head->next==NULL || head->next->next==NULL) return;
    a=head;
    b=head->next;

    int tmp=a->x;
    a->x=b->x;
    b->x=tmp;

    if(b->next!=NULL)
        swp(b->next);
    return;
}

main()
{
    llist *lst, *tr;
    lst=new llist;
    tr=lst;
    cout<<"Enter number of nodes: ";
    int n,x;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        lst->x=x;
        lst->next=new llist();
        lst=lst->next;
    }

    swp(tr);
    cout<<endl;

    while(tr->next!=NULL)
    {
        cout<<tr->x<<" ";
        tr=tr->next;
    }
}

input-output:
Enter number of nodes: 5
2 5 9 6 7

5 2 6 9 7

- raihanruhin August 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code:

void SwapNodes(Node** start)
{
	if (start == NULL || *start == NULL)
		return;

	Node* temp = *start;
	if (temp ->next != null)
	{
		*start = temp->next;
		while (temp && temp->next)
		{
			Node* next = temp->next->next;
			Node* temp2 = temp->next;
			temp2->next = temp;
			temp->next = next;
			temp = next;
		}
	}
}

- coding.arya August 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct ListNode 
{ 
	int val;
	ListNode *next;
};


ListNode* swap(ListNode *head)
{
	if (!head) 
		return NULL;

	ListNode *node = head;
	ListNode *next = node->next;
	if (!next) 
		return head;
	
	head = next;
	ListNode *prev = NULL;
	do
	{
		node->next = next->next;
		next->next = node;
		if (prev)
			prev->next = next;
		prev = node;
  
	} while ((node=node->next) && (next=node->next));

	return head;
} 
void main(void)
{

	ListNode *h = new ListNode;
	h->val = 1; 
	h->next = NULL;

	ListNode *t = h;
	for (int i=2; i<=13; ++i)
	{
		t->next = new ListNode;
		t->val = i; 
		t = t->next;
	}
	t->next = NULL;

	ListNode *n = NULL; 
	for (n=h; n; n=n->next)
		cout << n->val << " " ;
	cout << endl;


	h = swap(h);
	for (n=h; n; n=n->next)
		cout << n->val << " " ;
	cout << endl;

	system("pause");
}

- If list has two many nodes, recursion may cause stack overflow. August 05, 2013 | 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