Program to convert linked list by alternating nodes from front and back in Python

Given a singly linked list, we need to rearrange it by alternating nodes from the back and front. The pattern is: last node, first node, second-last node, second node, and so on.

For example, if the input is [1,2,3,4,5,6,7,8,9], the output will be [9, 1, 8, 2, 7, 3, 6, 4, 5].

Algorithm

To solve this problem, we follow these steps ?

  • Store all node values in a list for easy access from both ends

  • Traverse the linked list again and assign values alternately from the back and front of the stored list

  • For odd positions (1st, 3rd, 5th...), take values from the end of the list

  • For even positions (2nd, 4th, 6th...), take values from the beginning of the list

Implementation

Let's implement the solution with a complete example ?

class ListNode:
    def __init__(self, data, next_node=None):
        self.val = data
        self.next = next_node

def make_list(elements):
    if not elements:
        return None
    
    head = ListNode(elements[0])
    current = head
    
    for element in elements[1:]:
        current.next = ListNode(element)
        current = current.next
    
    return head

def print_list(head):
    result = []
    current = head
    while current:
        result.append(str(current.val))
        current = current.next
    print("[" + ", ".join(result) + "]")

class Solution:
    def solve(self, node):
        if not node:
            return node
        
        # Store all values in a list
        values = []
        current = node
        while current:
            values.append(current.val)
            current = current.next
        
        # Rearrange by alternating from back and front
        current = node
        while current and values:
            # Take from back (last element)
            current.val = values.pop()
            current = current.next
            
            if current is None:
                break
            
            # Take from front (first element)
            current.val = values.pop(0)
            current = current.next
        
        return node

# Example usage
ob = Solution()
head = make_list([1, 2, 3, 4, 5, 6, 7, 8, 9])
result = ob.solve(head)
print_list(result)
[9, 1, 8, 2, 7, 3, 6, 4, 5]

How It Works

The algorithm works in two passes ?

  1. First pass: Extract all values from the linked list into a Python list for easy manipulation

  2. Second pass: Traverse the linked list again and assign values alternately using pop() (from end) and pop(0) (from beginning)

The pattern alternates between taking the last remaining element and the first remaining element from our stored values.

Time and Space Complexity

  • Time Complexity: O(n²) due to pop(0) operations on the list

  • Space Complexity: O(n) for storing the values in the auxiliary list

Conclusion

This solution rearranges a linked list by alternating nodes from the back and front using an auxiliary list. While simple to implement, the pop(0) operation makes it less efficient for large lists, but it clearly demonstrates the alternating pattern concept.

Updated on: 2026-03-25T12:06:03+05:30

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements