Print a Singly Linked List in Spiral Order Using Python

What is a Singly Linked List?

A linear data structure in which the data elements can be stored as an order set in a non-contagious memory location is called the singly linked list. It contains two parts: the data part and the address part. The data part of the node contains information about it, and the address part contains the address of the element. Each element is connected only to the data part of the next element using a pointer. It can be traversed only in one direction. Each node has only the next pointer, so it cannot be traversed in reverse order.

Problem Statement

We have a linked list. We need to print the singly list in a spiral order, meaning it must start from the mid and rotate clockwise.

Let's understand this problem statement with a few examples.

Example 1:

Input:

Output:

7 - > 2 - > 4 - > 1 - > 5 - > 6

Print a Singly Linked List in Spiral Order Using Python

Explanation:

We have to print the linked list in a spiral order, i.e., it must start with the mid element and then move clockwise. As it has even elements, we must decide on the mid element. We have taken 7 as the mid element. It will be the first element of the spiral singly linked list. Then, it moves to the adjacent element, i.e., 2. Then, moving clockwise, it will move in this order: 4, 1, 5, and then 6.

Example 2:

Input:

Output:

13 - > 29 - > 44 - > 10 - > 23

Print a Singly Linked List in Spiral Order Using Python

Explanation:

Again, in this linked list, we have to print it in spiral order. As there are odd elements in the linked list, the mid element will be 13. Starting from 13, it will move clockwise in the order 29, 44, 10, and then 23.

There are different approaches to printing a singly linked list in spiral order.

1. Naive Approach

This is the simplest approach to print the singly linked list in the spiral order. In this, we will store the linked list data in an array, traverse its elements using their index in spiral order, and then print it.

The basic algorithm for this approach:

  • First, create an array.
  • Then, traverse the linked list.
  • Add the data into the array.
  • Again, traverse it in a spiral order using two pointers. One pointer is from the mid element to this left element, and the second is from the mid element to the end element.

2. Reverse Traversal Method

This is a more efficient and simple approach to printing the singly linked list in a spiral order. Firstly, we will find the mid element of the linked list, then reverse it from the starting element to the mid element. Then, we will traverse it backwards from the mid element to the starting element. At last, traverse the mid element to the end element in the forward direction.

The basic algorithm for this approach:

  • Firstly, find the mid element of the list to start the spiral list.
  • Then, reverse the list from the starting to the mid element to traverse it backwards.
  • Now, using two pointers, traverse the list. One pointer is for the mid element, which moves to the left, and another is for the next element of the mid element (mid + 1) to the right.

Let's implement the code to convert a linked list into a spiral order using Python

Code:

Output:

Linked List (even elements) :
5 --> 4 --> 7 --> 2 --> 1 --> 6 
Spiral Linked List (even elements) :
2 --> 7 --> 1 --> 4 --> 6 --> 5 
Linked List (odd elements):
23 --> 44 --> 13 --> 29 --> 10
Spiral Linked List (odd elements) :
13 --> 29 --> 44 --> 10 --> 23 

Explanation:

With this code, we printed the linked list in spiral order for odd and even number of nodes. First, we made a class LinkedList and inserted nodes into it. Then, with the help of pointers, we have made a function called Spiral( ), which converts the linked list into spiral order. Then, using the display( ) function, we printed the spiral linked list.