Bird
0
0
DSA Cprogramming~30 mins

Reorder Linked List in DSA C - Build from Scratch

Choose your learning style9 modes available
Reorder Linked List
📖 Scenario: You are working on a program that manages a list of tasks. The tasks are stored in a linked list in the order they were added. Sometimes, you want to reorder the list so that the first task is followed by the last task, then the second task, then the second last task, and so on.This helps to balance the tasks between the start and the end of the list.
🎯 Goal: Build a program that reorders a singly linked list in the pattern: first node, last node, second node, second last node, and so on.For example, if the list is 1 -> 2 -> 3 -> 4 -> 5 -> NULL, after reordering it should become 1 -> 5 -> 2 -> 4 -> 3 -> NULL.
📋 What You'll Learn
Create a singly linked list with nodes containing integer values.
Find the middle of the linked list.
Reverse the second half of the linked list.
Merge the two halves by alternating nodes.
Print the reordered linked list.
💡 Why This Matters
🌍 Real World
Reordering linked lists is useful in scheduling tasks, balancing workloads, or rearranging data for better access patterns.
💼 Career
Understanding linked list manipulation is important for software engineers working on low-level data structures, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the linked list
Create a struct called Node with an int data and a Node* called next. Then create 5 nodes with values 1, 2, 3, 4, 5 linked together in that order. Store the head of the list in a variable called head.
DSA C
Hint

Start by defining the Node struct with data and next. Then create each node with malloc and link them by setting the next pointers.

2
Find the middle of the linked list
Add code to find the middle node of the linked list using two pointers called slow and fast. Initialize both to head. Move slow by one step and fast by two steps until fast reaches the end or fast->next is NULL. After the loop, slow should point to the middle node.
DSA C
Hint

Use two pointers starting at the head. Move slow by one node and fast by two nodes in a loop until fast reaches the end.

3
Reverse the second half of the list
Reverse the linked list starting from the node pointed to by slow. Use three pointers: prev initialized to NULL, curr initialized to slow, and next for temporary storage. Iterate until curr is NULL, reversing the next pointers. After the loop, prev will be the head of the reversed second half. Store it in a variable called second.
DSA C
Hint

Use a loop to reverse the list starting from slow. Keep track of previous, current, and next nodes to reverse the links.

4
Merge the two halves and print the reordered list
Merge the first half starting from head and the reversed second half starting from second by alternating nodes. Use two pointers first and second. In each iteration, save the next nodes, link first->next to second, then second->next to the saved next of first. Move first and second forward. After merging, print the reordered list by traversing from head and printing each node's data followed by ->. End with NULL.
DSA C
Hint

Alternate nodes from the first half and reversed second half by adjusting next pointers. Then print the list by traversing from head.