To move the last element of a linked list to the front, you can follow these steps:
- Traverse the linked list to find the last node.
- Set the
next
pointer of the second-to-last node to null to remove the last node from its current position. - Set the
next
pointer of the last node to the current head of the linked list. - Set the head of the linked list to the last node.
Here’s an implementation of this algorithm in Kotlin:
class Node(var value: Int) {
var next: Node? = null
}
fun moveLastToFront(head: Node?): Node? {
// Return the original list if it's empty or has only one node
if (head == null || head.next == null) {
return head
}
var curr = head
var prev: Node? = null
// Traverse the list to find the last node and its previous node
while (curr?.next != null) {
prev = curr
curr = curr.next
}
// Remove the last node from its current position
prev?.next = null
// Set the last node to the current head of the list
curr?.next = head
// Set the head of the list to the last node
return curr
}
fun main() {
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5
val head = Node(1)
head.next = Node(2)
head.next?.next = Node(3)
head.next?.next?.next = Node(4)
head.next?.next?.next?.next = Node(5)
// Print the original linked list
printLinkedList(head)
// Move the last node to the front
val newHead = moveLastToFront(head)
// Print the modified linked list
printLinkedList(newHead)
}
fun printLinkedList(head: Node?) {
var curr = head
while (curr != null) {
print("${curr.value} ")
curr = curr.next
}
println()
}
Step-by-step explanation of the moveLastToFront
function:
- The function takes a linked list
head
as input and returns the modified linked list. - If the linked list is empty or has only one node, we don’t need to do anything. In this case, we simply return the original list.
- We initialize a variable
curr
to the head of the linked list. This variable will be used to traverse the linked list to find the last node. - We start traversing the linked list from the head and move forward until we reach the last node. We keep track of the previous node using a variable
prev
. We also update thecurr
variable at each iteration to move forward in the list. - After the traversal is complete,
curr
will point to the last node in the list, andprev
will point to the second-to-last node. We remove the last node from its current position by setting thenext
pointer ofprev
to null. - We then set the
next
pointer ofcurr
to the current head of the list. This makes the last node the new head of the list. - Finally, we return the new head of the list.
The time complexity of the moveLastToFront
function is O(n), where n is the number of nodes in the linked list. This is because we need to traverse the linked list once to find the last node, and this operation takes linear time in the size of the list.
The space complexity of the function is O(1), because we only use a constant amount of additional memory to store the curr
and prev
variables, regardless of the size of the input list. Therefore, the space used by the function does not depend on the size of the list, and it is considered constant.