May 18th, 2020's Question
Shift Linked List
Here's a nice question that has multiple ways to answer it.
Write a function that takes in the head of a linked list and a integer k, shifts the list in place by k positions and returns its new head.a
Shifting a linked list means moving its nodes forward or backward and wrapping them around the list where appropriate.
Whether the nodes are moved left or right is determined by whether or not k is positive or negative.
Example:
head = 0 -> 1 -> 2 -> 3 -> 4 -> 5
k = 2
Answer: 4 -> 5 -> 0 -> 1 -> 2 -> 3
Here's what the method signature looks like:
public static LinkedList shiftLinkedList(LinkedList head, int k) {
}
If you think you have the answer, try to code it out and come back when you think you've figured it out
You got this!
See Answer and Explanation