Knowee
Questions
Features
Study Tools

Alternate Elements in Linked ListYou are now well versed with the concepts of Linked Lists. So, now you have been given an advance coding challenge, i.e., you have to create the code on the topic linked lists such that it will give the output of only elements in alternate position from last to first and then first to last. Constraints:Input Format :First line of input contains the number of integers.Next lines are followed by the integer values.Output Format :Corresponding output.Example:Input 1:51  2  3  4  5Output 1:1  3  55  3  1 Input 2:411  12  14  13Output 2:12   1313  12  Explanation:Alternate elements from first element are 1 3 5Alternate elements from last to first are 5 3 1Public Test Cases:# INPUT EXPECTED OUTPUT1 51 2 3 4 51 3 5 5 3 1 2 4 11 12 14 1311 14 13 12

Question

Alternate Elements in Linked ListYou are now well versed with the concepts of Linked Lists. So, now you have been given an advance coding challenge, i.e., you have to create the code on the topic linked lists such that it will give the output of only elements in alternate position from last to first and then first to last. Constraints:Input Format :First line of input contains the number of integers.Next lines are followed by the integer values.Output Format :Corresponding output.Example:Input 1:51  2  3  4  5Output 1:1  3  55  3  1 Input 2:411  12  14  13Output 2:12   1313  12  Explanation:Alternate elements from first element are 1 3 5Alternate elements from last to first are 5 3 1Public Test Cases:# INPUT EXPECTED OUTPUT1 51 2 3 4 51 3 5 5 3 1 2 4 11 12 14 1311 14 13 12

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The problem is asking to print alternate elements of a linked list from the start to the end and then from the end to the start.

Here is a step by step solution in Python:

  1. First, we need to create a linked list from the given input. We can create a Node class and a LinkedList class for this purpose.
class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
  1. Next, we need to add a method in the LinkedList class to add elements to the linked list.
def append(self, data):
    if not self.head:
        self.head = Node(data)
    else:
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = Node(data)
  1. Now, we need to add a method to print alternate elements from the start to the end. We can do this by traversing the linked list and printing the data of every second node.
def print_alternate_start_to_end(self):
    curr = self.head
    count = 0
    while curr:
        if count % 2 == 0:
            print(curr.data, end=' ')
        curr = curr.next
        count += 1
  1. Similarly, we need to add a method to print alternate elements from the end to the start. We can do this by first reversing the linked list and then printing the data of every second node.
def print_alternate_end_to_start(self):
    self.reverse()
    self.print_alternate_start_to_end()

def reverse(self):
    prev = None
    curr = self.head
    while curr:
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node
    self.head = prev
  1. Finally, we can create a linked list from the input, call the print_alternate_start_to_end method, and then call the print_alternate_end_to_start method.
ll = LinkedList()
for i in input().split():
    ll.append(int(i))
ll.print_alternate_start_to_end()
print()
ll.print_alternate_end_to_start()

This will print the alternate elements of the linked list from the start to the end and then from the end to the start.

This problem has been solved

Similar Questions

reverse a linked list- DATA STRUCTURESWrite a program to reverse a linked list.Constraints:Integers onlyExample:Input FormatTo get the number of elements followed by the elements separated by a single space.Output FormatDisplays the reversed list.Note: There is a space at the end of the outputExplanation:linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 3 67 45 1212 45 672 5 -1 3 2 6 44 6 2 3 -1

Reversal ChallengeImagine you are working as an employee in an MNC, where your manager knows that you are very good at coding in arrays. He has provided you a vigorous training on data structures, so he challenges you to do the reversal of an elements using linked lists.Constraints:Input Format :The input contains a set of integers.Output Format :The output contains reversed set of given integers.Example:Input 1:51  2  3  4  5Output 1:5  4  3  2  1Input 2:411  12  14  13Output 2:13  14  12  11Explanation:Reverse the elements sequence in the given linked listPublic Test Cases:# INPUT EXPECTED OUTPUT1 5 1 2 3 4 5 5 4 3 2 12 4 11 12 14 1313 14 12 11

Given a linked list with odd and even numbers, change the list in such a way that the even numbers appear first followed by the odd numbersDonot change the value.Sample Input:12 34 13 57 19 48 29 10 28 58 -1Sample Output:12 34 48 10 28 58 13 57 19 29

Given a linked list of size N. The task is to reverse every k nodes (where k is an input to the function) in the linked list.Input FormatFirst line of input contains n- the size of first linked list. Next line of input contains n integers the n elements of the first linked list.Constraints1 <= N <= 10^41 <= k <= NOutput FormatPrint the modified linked list.Sample input81 2 2 4 5 6 7 84Sample output4 2 2 1 8 7 6 5

Write a Python program to interchange the first and last element of the ListConstraints:Input Format:Line 1: <Read a List>Output Format:<List>Example:Input:[5,10,15,20,25,30,35,40]Output:[40, 10, 15, 20, 25, 30, 35, 5]Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 [5,10,15,20,25,30,35,40][40, 10, 15, 20, 25, 30, 35, 5]

1/4

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.