Table of Contents
Shift Linked List Problem is easier to approach with a clear overview and reliable steps. This guide organizes the essential information, highlights practical details, and explains what to check along the way.
Key Takeaways
- Review i. Introduction.
- Explore II. Topic.
- Understand III. Solution 1: connect the linked list into a loop.
I. Introduction
In the previous part, I introduced you to the classic algorithm question - Reverse linked list. Now I will introduce another classic question about linked lists, which is shift linked list. In this article, I will give 2 ways to solve the problem and analyze the space and time complexity of each way. I will assume that you already know about the linked list data structure when reading this article. If not, you can read the document "Basic linked list" of Stanford University, USA here.
II. Topic
Write a function that takes the head of a Singly Linked List and a number k, shifts the list in place k positions, and returns the new head after shifting. The number k can be positive or negative; this means you have to handle two cases: shifting the linked list forward and backward.
Input 1:
Output 1:
Input 2:
Output 2:
In the following sections, I will give two ways to solve this problem.
III. Solution 1: Connect the Linked List into a Loop
We will join linked list into a loop linked list then find new head and break the loop at new head to create new linked list. This helps you be able to shift linked list to create a new linked list. Below is the description of my solution:

The detailed steps are as follows:
Use the two pointers approach: One pointer points to the head, the other pointer moves to the tail of the linked list. For instance, if the length of the linked list is 6, k = 2, the new head will be the 2nd node from the tail; if k = 8, the new head will also be the 2nd node from the tail. Then attach tail.next = head to create a loop if k > 0: the new head will be the (k % length)th node from the tail. If k < 0: the new head will be the ( |k| % length )th node from the head. For instance, if the length of the linked list is 6, k = -2, the new head will be the 2nd node from the head. We break the loop into a normal linked list by attaching new_tail.next = None.
Here is my solution to this approach:
Space and Time Complexity Analysis
Note: n is the number of nodes of the input linked list.
Space Complexity Is O(1):
Since the entire algorithm processes directly on the input linked list, the space complexity is constant - O(1).
Time Complexity Is O(n):
Time complexity for pointer to reach tail node is O(n).
Time complexity to join linked list is O(1).
Time complexity to find new head in worst case is O(n).
Time complexity to break linked list loop is O(1).
Therefore, the total time complexity is O(1).
Approach 2: Use List as Intermediary
Similar to linked list, we can solve the linked list problem through lists and then reconstruct a linked list. Here is my description:

The detailed steps are as follows:
We convert from linked list to list Based on the shift properties mentioned in approach 1, we calculate magic number = k % length Then, perform shift on this list with the format list[-magic:] + list[:-magic] Rebuild the linked list on the shifted list.
My solution to this approach:
Space and Time Complexity Analysis:
Space Complexity Is O(n):
Space complexity to convert from linked list to list is O(n) Space complexity to construct linked list from new list is O(n) Therefore, total space complexity is O(n).
Time Complexity Is O(n):
Time complexity to convert from linked list to list is O(n).
Time complexity to build linked list from new list is O(n).
Therefore, the total time complexity is O(n).
III. Conclusion
The above two solutions are not all the ways to shift a linked list but are two quite typical ways for this classic problem. Hopefully this blog post can help you in some way. Please share it so that more people know about it if you find the blog post interesting. And look forward to more of my blog posts on the topic of Data Structure & Algorithm on this guide's tech blog!
FAQ
What should I know first about Shift Linked List Problem?
Introduction In the previous part, I introduced you to the classic algorithm question - Reverse linked list.
How do I get the best results with Shift Linked List Problem?
Use current software or equipment, follow the steps in order, review the recommended settings, and test one change at a time so you can identify what improves the result.
What should I do if Shift Linked List Problem doesn't work as expected?
Check compatibility, permissions, connectivity, and version-specific settings. Restart the relevant device or app, then repeat the process carefully before trying a more advanced fix.
Reader Comments 0
Sign in with email or Google to join the discussion.