ExploreNeedy      
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?
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