Print a Singly Linked List in Spiral Order Using PythonWhat 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 StatementWe 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 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 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 ApproachThis 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:
2. Reverse Traversal MethodThis 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:
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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India