Alternatingly Merge Lists

easy
1. You are given two singly linked list with N and M nodes respectively.
 2. You have to write a function that which inserts the nodes of second list
    into first list at alternate positions of first list. For example,
    Input:
    List1- 5->7->17->13->11
    List2- 12->10->2->4->6
 
    Output:
    List1- 5->12->7->10->17->2->13->4->11->6
    List2- (empty)
 
    Note: The nodes of second list should only be inserted when there are
          positions available.
 
    For example:
    Input:
    List1- 1->2->3
    List2- 4->5->6->7->8
    Output:
    List1- 1->4->2->5->3->6
    List2- 7->8
 
 3. display is a utility function which displays the contents of Linked List,
    feel free to use it for debugging purposes.
 4. main takes input from the user and creates the Linked Lists. You can use
    display to know its contents.
 5. This is a functional problem. 
 6. You should code only the mergeAlt function. It takes as input the
    heads of the first and second linked list respectively. It should return 
    an array of type Node (size 2), containing head of first list at 0th index
    and head of second list at index 1. 
 7. Don't change the code of Node, main and display.

Input Format

First line takes N, the number of elements in the first list. Second line takes input N space separated numbers reperesenting elements of the first linked list. Third line takes M, the number of elements in the second list. Fourth line takes input M space separated numbers reperesenting elements of the second linked list. Input is handled for you.

Output Format

Resultant list 1 Resultant list 2 Output is handled for you.

Constraints

1 <= N <= 1000
 1 <= M <= 1000

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
2
9 10
6
5 4 3 2 1 6
Output
9 5 10 4 
3 2 1 6
Previous
Add One To Linked List
Next
Loop Detection In A Linked List

Related Questions