Print Nodes Of Linked List At Given indexes

Problem Statement

Given two singly linked lists l1 and l2. l2 is a singly linked list containing various indices in sorted order. We need to print the data in l1 at indices mentioned in l2. (indices are 1 based).



5 10 1

Problem Statement Understanding

Let’s understand this problem with the above example. We have 5 -> 18 -> 9 -> 10 -> 1 -> 73 -> 11 as l1 the given linked list and 1 -> 4 -> 5 as l2 the linked list containing the indices to print.

Lets mark the indices of the nodes of given linked list l1.

Now let’s look at the required list of indices and find the corresponding nodes.
The given list of indices l2 is: 1 -> 4 -> 5
Node at index 1 = 5
Node at index 4 = 10
Node at index 5 = 1

So we print 5 10 1 as the output.


I hope you got a basic idea of what we need to do to solve this problem. The idea is simple, since the linked list containing indices (l2) is sorted, we just need to iterate through the linked list containing values (l1) while keeping track of the current index and the index to print. Whenever the current index and index to print match, we print the value at that index and update the index to print with the next node in the linked list l2, and move to the next node in l2.

Since it is clear what we need to do, take some time and think about how we are going to do it.


  • Declare 2 variables curr_indx and indx_to_print and initialize them as curr_indx = 1 and indx_to_print = l2->val i.e. the first index to print.
  • Now iterate through the linked list l1 till we reach the end of either of the linked list.
  • In each iteration compare the value of curr_indx with indx_to_print. If they match
  • Print the value of the current node in l1.
  • Move to the next node in l2
  • If there are more nodes in l2 (i.e. l2!=NULL), then update the value in indx_to_print with the value in l2.
  • Increment the curr_indx by 1.

Dry Run


using namespace std;

struct Node {
    int val;
    Node* next;

    Node(int value){
        val = value;
        next = NULL;

void push_front(Node** head, int new_val){
    Node* new_node = new Node(new_val);
    new_node->next = *head;
    *head = new_node;

void printAtIndices(Node* l1, Node* l2){
    int curr_index = 1;
    int indx_to_print = l2->val;

    while(l1!=NULL && l2!=NULL){
        if(curr_index == indx_to_print){
            cout<val<<" ";
            l2 = l2->next;
            if(l2!=NULL) indx_to_print = l2->val;
        l1 = l1->next;
        curr_index ++;

int main(){
    Node* l1 = NULL;
    push_front(&l1, 11);
    push_front(&l1, 73);
    push_front(&l1, 1);
    push_front(&l1, 10);
    push_front(&l1, 9);
    push_front(&l1, 18);
    push_front(&l1, 5);
    // 5 18 9 10 1 73 11
    Node* l2 = NULL;
    push_front(&l2, 5);
    push_front(&l2, 4);
    push_front(&l2, 1);
    // 1 4 5

    // 5 10 1


5 10 1

Time complexity: O(n), where n is the length of the linked list.
Space complexity: O(1), as we aren’t using any extra space.

Through this article, we learned how to print the nodes of a linked list at given indices. Problems like these are good for strengthening your concepts in LinkedList. I would highly recommend you to practice more such problems from Linked List.

Previous post Student Record Management System Using Linked List
Next post Find Length Of A Linked List Iterative And Recursive

Leave a Reply

Your email address will not be published. Required fields are marked *