原文:

给定一个按升序排序的单链表l1和另一个未排序的单链表l2。 任务是根据数据在第一链表的节点中指出的位置来打印第二链表的元素。 例如,如果第一个链表是1 -> 2 -> 5,则必须打印第二个链表的第一、第二和第五个元素。

示例

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 

用两个指针使用两个嵌套循环遍历两个链表。 外循环分别指向第一个列表的元素,内循环分别指向第二个列表的元素。 在外循环的第一次迭代中,指向第一个链表头的指针指向其根节点。 我们遍历第二个链表,直到到达第一个链表中节点数据所指出的位置。 到达所需位置后,打印第二个列表的数据并再次重复此过程,直到到达第一个链表的末尾。

下面是上述方法的实现:

c

// c   program to print second linked list 
// according to data in the first linked list 
#include  
using namespace std; 
// linked list node 
struct node { 
    int data; 
    struct node* next; 
}; 
/* function to insert a node at the beginning */
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; 
} 
// function to print the second list according 
// to the positions referred by the 1st list 
void printsecondlist(node* l1, node* l2) 
{ 
    struct node* temp = l1; 
    struct node* temp1 = l2; 
    // while first linked list is not null. 
    while (temp != null) { 
        int i = 1; 
        // while second linked list is not equal 
        // to the data in the node of the 
        // first linked list. 
        while (i < temp->data) { 
            // keep incrementing second list 
            temp1 = temp1->next; 
            i  ; 
        } 
        // print the node at position in second list 
        // pointed by current element of first list 
        cout << temp1->data << " "; 
        // increment first linked list 
        temp = temp->next; 
        // set temp1 to the start of the 
        // second linked list 
        temp1 = l2; 
    } 
} 
// driver code 
int main() 
{ 
    struct node *l1 = null, *l2 = null; 
    // creating 1st list 
    // 2 -> 5 
    push(&l1, 5); 
    push(&l1, 2); 
    // creating 2nd list 
    // 4 -> 5 -> 6 -> 7 -> 8 
    push(&l2, 8); 
    push(&l2, 7); 
    push(&l2, 6); 
    push(&l2, 5); 
    push(&l2, 4); 
    printsecondlist(l1, l2); 
    return 0; 
} 

java

// java program to print second linked list  
// according to data in the first linked list  
class gfg 
{ 
// linked list node  
static class node 
{  
    int data;  
    node next;  
};  
/* function to insert a node at the beginning */
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;  
}  
// function to print the second list according  
// to the positions referred by the 1st list  
static void printsecondlist(node l1, node l2)  
{  
    node temp = l1;  
    node temp1 = l2;  
    // while first linked list is not null.  
    while (temp != null) 
    {  
        int i = 1;  
        // while second linked list is not equal  
        // to the data in the node of the  
        // first linked list.  
        while (i < temp.data)  
        {  
            // keep incrementing second list  
            temp1 = temp1.next;  
            i  ;  
        }  
        // print the node at position in second list  
        // pointed by current element of first list  
        system.out.print( temp1.data   " ");  
        // increment first linked list  
        temp = temp.next;  
        // set temp1 to the start of the  
        // second linked list  
        temp1 = l2;  
    }  
}  
// driver code  
public static void main(string args[])  
{  
    node l1 = null, l2 = null;  
    // creating 1st list  
    // 2 . 5  
    l1 = push(l1, 5);  
    l1 = push(l1, 2);  
    // creating 2nd list  
    // 4 . 5 . 6 . 7 . 8  
    l2 = push(l2, 8);  
    l2 = push(l2, 7);  
    l2 = push(l2, 6);  
    l2 = push(l2, 5);  
    l2 = push(l2, 4);  
    printsecondlist(l1, l2);  
} 
}  
// this code is contributed by arnab kundu 

python3

# python3 program to prsecond linked list 
# according to data in the first linked list 
# linked list node 
class new_node:  
    # constructor to initialize the node object  
    def __init__(self, data):  
        self.data = data  
        self.next = none
''' function to insert a node at the beginning '''
def push(head_ref, new_data): 
    new_node = new_node(new_data) 
    new_node.next = head_ref 
    head_ref = new_node 
    return head_ref 
# function to prthe second list according 
# to the positions referred by the 1st list 
def printsecondlist(l1,l2): 
    temp = l1 
    temp1 = l2 
    # while first linked list is not none. 
    while (temp != none): 
        i = 1
        # while second linked list is not equal 
        # to the data in the node of the 
        # first linked list. 
        while (i < temp.data): 
            # keep incrementing second list 
            temp1 = temp1.next
            i  = 1
        # prthe node at position in second list 
        # pointed by current element of first list 
        print(temp1.data,end=" ") 
        # increment first linked list 
        temp = temp.next
        # set temp1 to the start of the 
        # second linked list 
        temp1 = l2 
# driver code 
l1 = none
l2 = none
# creating 1st list 
# 2 . 5 
l1 = push(l1, 5) 
l1 = push(l1, 2) 
# creating 2nd list 
# 4 . 5 . 6 . 7 . 8 
l2 = push(l2, 8) 
l2 = push(l2, 7) 
l2 = push(l2, 6) 
l2 = push(l2, 5) 
l2 = push(l2, 4) 
printsecondlist(l1, l2) 
# this code is contributed by shubhamsingh10 

c

// c# program to print second linked list  
// according to data in the first linked list  
using system; 
class gfg  
{  
// linked list node  
public class node  
{  
    public int data;  
    public node next;  
};  
/* function to insert a node at the beginning */
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;  
}  
// function to print the second list according  
// to the positions referred by the 1st list  
static void printsecondlist(node l1, node l2)  
{  
    node temp = l1;  
    node temp1 = l2;  
    // while first linked list is not null.  
    while (temp != null)  
    {  
        int i = 1;  
        // while second linked list is not equal  
        // to the data in the node of the  
        // first linked list.  
        while (i < temp.data)  
        {  
            // keep incrementing second list  
            temp1 = temp1.next;  
            i  ;  
        }  
        // print the node at position in second list  
        // pointed by current element of first list  
        console.write( temp1.data   " ");  
        // increment first linked list  
        temp = temp.next;  
        // set temp1 to the start of the  
        // second linked list  
        temp1 = l2;  
    }  
}  
// driver code  
public static void main()  
{  
    node l1 = null, l2 = null;  
    // creating 1st list  
    // 2 . 5  
    l1 = push(l1, 5);  
    l1 = push(l1, 2);  
    // creating 2nd list  
    // 4 . 5 . 6 . 7 . 8  
    l2 = push(l2, 8);  
    l2 = push(l2, 7);  
    l2 = push(l2, 6);  
    l2 = push(l2, 5);  
    l2 = push(l2, 4);  
    printsecondlist(l1, l2);  
}  
}  
// this code has been contributed by 29ajaykumar 

输出

5 8


如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。