ExploreNeedy      
Enter a long URL to make short:
Share with -
  Feeds
  Technical
  Logical
  Puzzles
  Lectures
  Guestbook
HITS FlyArkaden.dk Free Web Counter

Linked List Questions


  1. How to declare a structure of a linked list in C/C++?
  2. Write an program to count number of nodes in a singly linked list?
  3. Reverse a Singly Linked List
  4. How to check whether a linked list has a loop/cycle.
  5. Detect a Cycle in a linked list and Fix the cycle
  6. How can you find the nodes with repetetive data in a linked list?
  7. Write a function to remove the nodes with repetetive data in a linked list?
  8. Write a function to swap every second node. [ie - 1->2->3->4->5->| becomes 2->1->4->3->5->|]
  9. Sort a linked list
  10. Write a function to Insert a node into sorted linked list
  11. Write a function to return Nth node from the end of the linked list
  12. How do you delete a node from linked list, if you have only a pointer to a node to be deleted?
  13. Write a C program to free all the nodes of a linked list?
  14. Implement a Generic Linked List. Means in the node data can be any thing int, char, float etc...
  15. Write a program to find the middle of a linked list?
  16. Write a program to compare two linked lists?
  17. Write a program to create a copy of a linked list?
  18. Write a C program to do a Binary search on a linked list?
  19. Remove duplicates from a sorted linked list.
  20. Read a singly linked list backwards?
Write an program to count number of nodes in a singly linked list?

int Length(struct node* head) {
    int count = 0;
    struct node* current = head;
    while (current != NULL) {
        count++;
        current = current->next;
    }
    return(count);
}

Write an efficient C/C++ program to reverse a singly linked list?

Node* ReverseList( Node * List ) {
    Node * list1 = List;
    Node * revList = NULL;
    Node * temp = NULL;
    while ( list1 ) {
        temp = list1;
        list1 = list1->pNext;
        temp->pNext = revList;
        revList = temp;
    }
    return revList;
}
Write an efficient C/C++ program to check whether a linked list has a loop

boolean CheckIsItCircular(Node *list) {
    Node * list1 = list;
    Node * list2 = list;
    while(list2) {
        list1 = list1 -> next;
        list2 = list2 -> next;
        if(list2) list2 = list2 -> next;
        else return false;
        (list1 == list2) ) return true;
    }

    return false;
}
Detect a Cycle in a linked list and Fix the cycle

How can you find the nodes with repetetive data in a linked list?

void findDuplicates(Node *list) {
    Node * first = list;
    Node * second;
    while (first) {
        second = first;
        second = second -> next;
        while (second) {
            if(second -> data == first -> data) {
                print ("%d", second ->data); \\assume data is integer
            }
            second = second -> next;
        }
        first = first -> next;
    }
}
Write a function to remove the nodes with repetetive data in a linked list?

void removeDuplicates(Node *list) {
    Node * first = list;
    Node * second;
    Node * pre;
    while (first) {
        second = first;
        pre = first;
        second = second -> next;
        while (second) {
            if(second -> data == first -> data) {
                pre -> next = second -> next;
                print ("%d", second ->data); \\ assume data is integer
                free (second);
                second = pre;
            }
            pre = second;
            second = second -> next;
        }
        first = first -> next;
    }
}
Write a function to swap every second node. [ie - 1->2->3->4->5->| becomes 2->1->4->3->5->|]

Recursive Method in C++:
List * SwapAlternate(List * head) {
    if(head == null) return head;
    if(head -> next == null) return head;
    List * temp = head->next;
    List * temp1 = null;
    temp1 = SwapAlternate(head->next->next);
    head -> next = temp1;
    temp -> next = head;
    return temp;
}
Sort a linked list

void Sort( Node *Head) {
    node* first,second,temp;
    first= Head;
    temp = new node();
    while(first!=null) {
        second=first->NEXT;
            while(second!=null) {
               if(first->value < second->value) {
                   temp->value=first->value;
                   first->value=second->value;
                   second->value=temp->value;
               }
            second=second->NEXT;
            }
        first=first->NEXT;
    }
    delete temp;
}

Write a function to Insert a node into sorted linked list

void sortedInsert(Node * head, Node* newNode) {
    Node *current = head;
    Node *pre = NULL;

    // traverse the list until you find item bigger the new node value
    while (current!= NULL && current->data <newNode->data) {
        pre = current;
        current = current->next;
    }

    // insert the new node before the big item

    newNode->next = current;
    if(pre != NULL)
        pre->next = newNode;
    else
        head = newNode;
}
Write a function to return Nth node from the end of the linked list

Node * GetNthNodeFromEnd ( Node* Head , int N ) {
    Node * pthNode = NULL;
    Node * tempNode = NULL;
    int currentElement = 0;

    for ( tempNode = Head; tempNode != NULL; tempNode = tempNode->Next ) {
        currentElement++;
        if ( currentElement - N == 0 ) 
            pthNode = Head;
        else if ( nCurrentElement - N > 0) 
            pthNode = pthNode ->Next;
    }

    if (pthNode) 
        return pthNode;
    else    
        return NULL;
}

Other improved Method
Node * GetNthNodeFromEnd ( Node* Head , int N ) {
    Node * temp=Head, *nthNodeFromEnd = NULL;
    int i;
    for(i=0; i<N-1 && temp; ++i) {
        temp = temp->next;
    }
    if(temp == null) {
        return NULL;
    }
    else {
        nthNodeFromEnd = Head;
        temp = temp -> next;
    }
    while(temp) {
        nthNodeFromEnd = nthNodeFromEnd->next;
        temp = temp->next;
    }
    return nthNodeFromEnd;
}

If you think that an important C/C++ Linked List interview questions or some answers are wrong in the site please contribute it to SiteAdmin@ExploreNeedy.com