How do you sort a linked list? Write a C program to sort a linked list.

October 28, 2007 · Filed Under Linked List, Placement Questions

This is a very popular interview question, which most people go wrong. The ideal solution to this problem is to keep the linked list sorted as you build it. Another question on this website teaches you how to insert elements into a linked list in the sorted order. This really saves a lot of time […]

Read the rest of this entry »

How to declare a structure of a linked list?

October 28, 2007 · Filed Under Linked List, Placement Questions

Home
How to declare a structure of a linked list?
The right way of declaring a structure for a linked list in a C program is
struct node {
int value;
struct node *next;
};
typedef struct node *mynode;
Note that the following are not correct
typedef struct {
int value;
mynode next;
} *mynode;
The typedef is not defined […]

Read the rest of this entry »

Write a C program to implement a Generic Linked List.

October 28, 2007 · Filed Under Linked List, Placement Questions

Here is a C program which implements a generic linked list. This is also one of the very popular interview questions thrown around. The crux of the solution is to use the void C pointer to make it generic. Also notice how we use function pointers to pass the address of different functions to print […]

Read the rest of this entry »

How do you reverse a linked list without using any C pointers?

October 28, 2007 · Filed Under Linked List, Placement Questions

One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the “next” pointer fields to traverse through […]

Read the rest of this entry »

How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.

October 28, 2007 · Filed Under Linked List, Placement Questions

This is also one of the classic interview questions
There are multiple answers to this problem. Here are a few C programs to attack this problem.
Brute force method
Have a double loop, where you check the node pointed to by the outer loop, with every node of the inner loop.
typedef struct node
{
void *data;
struct […]

Read the rest of this entry »

How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.

October 28, 2007 · Filed Under Linked List, Placement Questions

This is also one of the classic interview questions
There are multiple answers to this problem. Here are a few C programs to attack this problem.
Brute force method
Have a double loop, where you check the node pointed to by the outer loop, with every node of the inner loop.
typedef struct node
{
void *data;
struct […]

Read the rest of this entry »

How do you find the middle of a linked list? Write a C program to return the middle of a linked list

October 28, 2007 · Filed Under Linked List, Placement Questions

Another popular interview question
Here are a few C program snippets to give you an idea of the possible solutions.
Method1 (Uses one slow pointer and one fast pointer)
#include
#include
typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode ;
void add_node(struct node **head, int value);
void print_list(char *listName, struct node *head);
void getTheMiddle(mynode *head);
// The main […]

Read the rest of this entry »

How to compare two linked lists? Write a C program to compare two linked lists.

October 28, 2007 · Filed Under Linked List, Placement Questions

Here is a simple C program to accomplish the same.
int compare_linked_lists(struct node *q, struct node *r)
{
static int flag;
if((q==NULL ) && (r==NULL))
{
flag=1;
}
else
{
[…]

Read the rest of this entry »

How to create a copy of a linked list? Write a C program to create a copy of a linked list.

October 28, 2007 · Filed Under Linked List, Placement Questions

Check out this C program which creates an exact copy of a linked list.
copy_linked_lists(struct node *q, struct node **s)
{
if(q!=NULL)
{
*s=malloc(sizeof(struct node));
(*s)->data=q->data;
(*s)->link=NULL;
[…]

Read the rest of this entry »

Write a C program to return the nth node from the end of a linked list.

October 28, 2007 · Filed Under Linked List, Placement Questions

Here is a solution which is often called as the solution that uses frames.
Suppose one needs to get to the 6th node from the end in this LL. First, just keep on incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in this case)
STEP 1 : 1(ptr1,ptr2) -> 2 […]

Read the rest of this entry »