• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Showing posts with label linked list. Show all posts
    Showing posts with label linked list. Show all posts

    Sunday, March 28, 2010

    The solution is to iterate down the list looking for the correct place to insert the new node. That could be the end of the list, or a point just before a node which is larger than the new node.

    Note that we assume the memory for the new node has already been allocated and a pointer to that memory is being passed to this function.



    // Special case code for the head end
    void linkedListInsertSorted(struct node** headReference, struct node* newNode)
    {
    // Special case for the head end
    if (*headReference == NULL || (*headReference)->data >= newNode->data)
    {
    newNode->next = *headReference;
    *headReference = newNode;
    }
    else
    {
    // Locate the node before which the insertion is to happen!
    struct node* current = *headReference;
    while (current->next!=NULL && current->next->data <>data)
    {
    current = current->next;
    }
    newNode->next = current->next;
    current->next = newNode;
    }
    }
    Great C datastructure question!

    The answer is ofcourse, you can write a C program to do this. But, the question is, do you really think it will be as efficient as a C program which does a binary search on an array?

    Think hard, real hard.

    Do you know what exactly makes the binary search on an array so fast and efficient? Its the ability to access any element in the array in constant time. This is what makes it so fast. You can get to the middle of the array just by saying array[middle]!. Now, can you do the same with a linked list? The answer is No. You will have to write your own, possibly inefficient algorithm to get the value of the middle node of a linked list. In a linked list, you loosse the ability to get the value of any node in a constant time.

    One solution to the inefficiency of getting the middle of the linked list during a binary search is to have the first node contain one additional pointer that points to the node in the middle. Decide at the first node if you need to check the first or the second half of the linked list. Continue doing that with each half-list.
    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 the linked list (So in effect, you are using the pointers, but you are not changing them when reversing the linked list).

    Sunday, February 21, 2010

    To access nodes in a singly linked list we need to sequentially traverse the list. Getting nth node from the end would mean – going to the end and start traversing back. But how do we traverse back ?

    We can’t and we don’t need to. There are better ways to do this.

    Algorithm
    Maintain two pointers – reference pointer and main pointer. Initialize both reference and main pointers to head. First move reference pointer to n nodes from head. Now move both pointers one by one until reference pointer reaches end. Now main pointer will point to nth node from the end. Return main pointer.

    Time Complexity: O(n)
    Space Complexity: O(1)