Print nodes of linked list at given indexes
Last Updated :
11 Jan, 2023
Given a singly linked list which is sorted in ascending order and another singly linked list which is unsorted. The task is to print the elements of the second linked list according to the position pointed out by the data in the nodes of the first linked list. For example, if the first linked list is 1->2->5 then you have to print the 1st, 2nd and 5th elements of the second linked list.
Examples:
Input: l1 = 1->2->5
l2 = 1->8->7->6->2->9->10
Output : 1->8->2
Elements in l1 are 1, 2 and 5.
Therefore, print 1st, 2nd and 5th elements of l2,
Which are 1, 8 and 2.
Input: l1 = 2->5
l2 = 7->5->3->2->8
Output: 5->8
Take two pointers to traverse the two linked lists using two nested loops. The outer loop points to the elements of the first list and the inner loop point to the elements of the second list respectively. In the first iteration of outer loop, the pointer to the head of the first linked list points to its root node. We traverse the second linked list until we reach the position pointed out by the node’s data in the first linked list. Once the required position is reached, print the data of the second list and repeat this process again until we reach the end of the first linked list.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printSecondList(Node* l1, Node* l2)
{
struct Node* temp = l1;
struct Node* temp1 = l2;
while (temp != NULL) {
int i = 1;
while (i < temp->data) {
temp1 = temp1->next;
i++;
}
cout << temp1->data << " " ;
temp = temp->next;
temp1 = l2;
}
}
int main()
{
struct Node *l1 = NULL, *l2 = NULL;
push(&l1, 5);
push(&l1, 2);
push(&l2, 8);
push(&l2, 7);
push(&l2, 6);
push(&l2, 5);
push(&l2, 4);
printSecondList(l1, l2);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printSecondList(Node l1, Node l2)
{
Node temp = l1;
Node temp1 = l2;
while (temp != null )
{
int i = 1 ;
while (i < temp.data)
{
temp1 = temp1.next;
i++;
}
System.out.print( temp1.data + " " );
temp = temp.next;
temp1 = l2;
}
}
public static void main(String args[])
{
Node l1 = null , l2 = null ;
l1 = push(l1, 5 );
l1 = push(l1, 2 );
l2 = push(l2, 8 );
l2 = push(l2, 7 );
l2 = push(l2, 6 );
l2 = push(l2, 5 );
l2 = push(l2, 4 );
printSecondList(l1, l2);
}
}
|
Python3
class new_Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = new_Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def printSecondList(l1,l2):
temp = l1
temp1 = l2
while (temp ! = None ):
i = 1
while (i < temp.data):
temp1 = temp1. next
i + = 1
print (temp1.data,end = " " )
temp = temp. next
temp1 = l2
l1 = None
l2 = None
l1 = push(l1, 5 )
l1 = push(l1, 2 )
l2 = push(l2, 8 )
l2 = push(l2, 7 )
l2 = push(l2, 6 )
l2 = push(l2, 5 )
l2 = push(l2, 4 )
printSecondList(l1, l2)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printSecondList(Node l1, Node l2)
{
Node temp = l1;
Node temp1 = l2;
while (temp != null )
{
int i = 1;
while (i < temp.data)
{
temp1 = temp1.next;
i++;
}
Console.Write( temp1.data + " " );
temp = temp.next;
temp1 = l2;
}
}
public static void Main()
{
Node l1 = null , l2 = null ;
l1 = push(l1, 5);
l1 = push(l1, 2);
l2 = push(l2, 8);
l2 = push(l2, 7);
l2 = push(l2, 6);
l2 = push(l2, 5);
l2 = push(l2, 4);
printSecondList(l1, l2);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data=0;
this .next= null ;
}
}
function push(head_ref, new_data)
{
let new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printSecondList(l1,l2)
{
let temp = l1;
let temp1 = l2;
while (temp != null )
{
let i = 1;
while (i < temp.data)
{
temp1 = temp1.next;
i++;
}
document.write( temp1.data + " " );
temp = temp.next;
temp1 = l2;
}
}
let l1 = null , l2 = null ;
l1 = push(l1, 5);
l1 = push(l1, 2);
l2 = push(l2, 8);
l2 = push(l2, 7);
l2 = push(l2, 6);
l2 = push(l2, 5);
l2 = push(l2, 4);
printSecondList(l1, l2);
</script>
|
Complexity Analysis:
- Time Complexity: O(n^2)
- As we are using nested loops and traversing the items of the second list for every element in the first list.
- Auxiliary Space: O(1)
- As constant extra space is used.
Another approach:
As we know that the data in the first list is sorted we can make use of this fact to traverse the second list.
- Make two pointers to head of both the lists
- Set a counter to 1
- Keep incrementing the counter and moving forward in the second list till the counter’s value is less to the data at current pointer in first list
- Print the data of second list pointer and increment the pointer to first list
- Repeat steps 3-4 until the pointer to 1st list is not NULL.
implementation:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printSecondList(Node* l1, Node* l2)
{
struct Node* temp1 = l1;
struct Node* temp2 = l2;
int counter = 1;
while (temp1 != NULL) {
while (counter < temp1->data) {
temp2 = temp2->next;
counter++;
}
cout << temp2->data << " " ;
temp1 = temp1->next;
}
}
int main()
{
struct Node *l1 = NULL, *l2 = NULL;
push(&l1, 5);
push(&l1, 2);
push(&l2, 8);
push(&l2, 7);
push(&l2, 6);
push(&l2, 5);
push(&l2, 4);
printSecondList(l1, l2);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printSecondList(Node l1, Node l2)
{
Node temp1 = l1;
Node temp2 = l2;
int counter = 1 ;
while (temp1 != null ) {
while (counter < temp1.data) {
temp2 = temp2.next;
counter++;
}
System.out.print(temp2.data + " " );
temp1 = temp1.next;
}
}
public static void main(String args[])
{
Node l1 = null , l2 = null ;
l1 = push(l1, 5 );
l1 = push(l1, 2 );
l2 = push(l2, 8 );
l2 = push(l2, 7 );
l2 = push(l2, 6 );
l2 = push(l2, 5 );
l2 = push(l2, 4 );
printSecondList(l1, l2);
}
}
|
Python3
class new_Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = new_Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def printSecondList(l1, l2):
temp1 = l1
temp2 = l2
counter = 1
while (temp1 ! = None ):
while (counter < temp1.data):
temp2 = temp2. next
counter + = 1
print (temp2.data, end = " " )
temp1 = temp1. next
l1 = None
l2 = None
l1 = push(l1, 5 )
l1 = push(l1, 2 )
l2 = push(l2, 8 )
l2 = push(l2, 7 )
l2 = push(l2, 6 )
l2 = push(l2, 5 )
l2 = push(l2, 4 )
printSecondList(l1, l2)
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printSecondList(Node l1, Node l2)
{
Node temp1 = l1;
Node temp2 = l2;
int counter = 1;
while (temp1 != null ) {
while (counter < temp1.data) {
temp2 = temp2.next;
counter++;
}
Console.Write(temp2.data + " " );
temp1 = temp1.next;
}
}
public static void Main()
{
Node l1 = null , l2 = null ;
l1 = push(l1, 5);
l1 = push(l1, 2);
l2 = push(l2, 8);
l2 = push(l2, 7);
l2 = push(l2, 6);
l2 = push(l2, 5);
l2 = push(l2, 4);
printSecondList(l1, l2);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data=0;
this .next= null ;
}
}
function push(head_ref, new_data)
{
let new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printSecondList(l1,l2)
{
let temp1 = l1;
let temp2 = l2;
let counter = 1;
while (temp1 != null )
{
while (counter < temp1.data)
{
temp2 = temp2.next;
counter++;
}
document.write( temp2.data + " " );
temp1 = temp1.next;
}
}
let l1 = null , l2 = null ;
l1 = push(l1, 5);
l1 = push(l1, 2);
l2 = push(l2, 8);
l2 = push(l2, 7);
l2 = push(l2, 6);
l2 = push(l2, 5);
l2 = push(l2, 4);
printSecondList(l1, l2);
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Although we are using nested loops but total number of operations is limited to O(n) only.
- Auxiliary Space: O(1)
- As constant extra space is used.
This approach was contributed by Abhijeet Kumar.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...