Question:

    Suppose you are given a Linked List as 45-> 123-> 87-> 11-> 53-> 24-> 412-> 22. And we have to find the nth node from the end.

    Input:- n = 3
    Output:- 24

    BRUTE FORCE APPROACH:-

    In this method, start with the first node and count how many nodes are there after that node. If the number of nodes are < n-1, then return saying, “fewer number of nodes in the list.” If the number of nodes are > n-1 then go to the next node. Continue this until the number of nodes after the current node are n-1.

    Time Complexity:- O(n2), for scanning the remaining list (from the current node) for each node.
    Space Complexity:- O(1)

    IMPROVING THE COMPLEXITY:-

    The complexity of the above problem can be improved by using a hash table. Consider the list:- 5-> 1-> 17-> 4-> NULL.

    In this approach, create a hash table whose entries are <position of node, node address>. That means, key is the position of the node in the list and the value is the address of that node.

    Position in List Address of node
    1 Address of 5 node
    2 Address of 1 node
    3 Address of 17 node
    4 Address of 4 node

    By the time we traverse the complete list(for creating hash table), we an find the list length. Let us say, the list length is M. To find the nth term from the end of the Linked List, we can convert this to M-n+1th from beginning. Since we already know the length of the list, its just a matter of returning M-n+1th key value from the hash table.

    Time Complexity:- Time for creating hash table. Therefore T(M) = O(M).
    Space Complexity:- O(M), because we are creating a hash table of size M.

    AN APPROACH WITHOUT USING THE HASH TABLE

    In the above solution, what actually we are doing is finding the size of the Linked List. That means, we are using hash table to find the size of the Linked list. We can find the length of the linked list just after starting at the head node and traversing the list. So, we can find the length of the list without creating the hash table. After finding the length compute M-n+1 and with one more scan we can get M-n+1th term from the beginning.

    Time Complexity:- Time for finding length + Time for finding M-n+1th node from beginning, Therefore T(n) = O(n) + O(n) ≈ O(n).
    Space Complexity:- O(1), since no hash table is created.

    SOLVING THE PROBLEM IN ONE SCAN (MOST EFFICIENT)

    Use 2 pointers:-

    • pNthNode
    • pTemp

    Both the nodes point to the HEAD of the list. pNthNode starts moving only after pTemp has moved n nodes forward. From there, both nodes move forward until pTemp reaches NULL.
    pNthNode now points at the nth node from the end.

    /*
    Defining a function to find the nth node from the end.
    */
    struct node * nthNodeFromEnd(struct node * head, int n)
    {
    	// we need to find the nth node from the end.
    	struct node * pTemp = head;
    	struct node * pNthNode = head;
    
    	int nCurrentElement = 0;
    
    	int counter = 0;
    
    	while(counter != n)
    	{
    		pTemp = pTemp -> next;
    		counter++;
    	}
    
    	while(pTemp != NULL)
    	{
    		pNthNode = pNthNode -> next;
    		pTemp = pTemp -> next;
    	}
    
    	return pNthNode;
    }
    

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

    A working demo can be downloaded here. You can open it in any of the text editor like notepad, gedit etc…

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Important operations on a Linked List

    by nikoo28
    6 minutes read

    We covered some of the basic operations on a Linked List in this post. Basic operations on a Linked List However, in order to create a functional Linked List, we need even more operations before we can proceed further. Some of these operations are:- Inserting a node in the beginning of the Linked List. Inserting a node in the middle of a List. Inserting at a certain position Inserting after a specific node. Deleting a node from the Linked List. Deleting the entire Linked List. Inserting a node in the …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Basic Operations on a Linked List

    by nikoo28
    5 minutes read

    Here are some of the basic operations on a singly Linked List. Creating a new List Adding new elements Traversing a List Printing a List Basic functions to perform the above techniques have been defined in the code below. Creating a new list A new Linked List creation means that we do not have any elements in the list and we want to start fresh. This means allocating space in the memory for a node and then inserting the data into it. Since only a single node is created, the …

    0 FacebookTwitterLinkedinWhatsappEmail
  • void binary(int n) { if(n < 1) printf("%s\n",A); // Assume A is a global variable else { A[n-1] = ‘0’; binary(n-1); A[n-1] = ‘1’; binary(n-1); } } So if you supply n = 4 in the function what you get as the output is this. 0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111 You can download the code here.

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    What is Backtracking?

    by nikoo28
    1 minutes read

    Backtracking is the method of exhaustive search using divide and conquer. Sometimes the best algorithm for a problem is to try all the possibilities. This is always slow, but there are standard tools that can be used for help. Tools: algorithms for generating basic objects, such as binary strings [2n possibilities for n-bit string], permutations [n!], combinations [n!/r!(n-r)!], general strings [k-ary strings of length  n has kn possibilities], etc… Backtracking speeds the exhaustive search by pruning. Example Algorithms of Backtracking Binary Strings: generating all binary strings Generating k-ary Strings. The …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Discuss the Tower Of Hanoi puzzle

    by nikoo28
    3 minutes read

    The tower of Hanoi is a mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks on one rod in ascending order of size, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, satisfying the following rules:- Only one disk can be moved at a time. Each move consists of taking the upper disk from one of the …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More