Interview Query

Reverse List Starting at Index K

Start Timer

0:00:00

Upvote
1
Downvote
Save question
Mark as completed
View comments (2)
Next question

You’re given the head node of a singly linked list and a natural number k. k will always be less than or equal to the length of the list, such that k <= len(SinglyLinkedList). Write a function reverse_at_position(head, position) that reverses the linked list starting from the index k (the index starts at 0) to the end of the list.

A class Node is defined as the following:

class Node:
    def __init__(self, element, next=None):
        self.element = element
        self.next = next

Note: After the reverse operations, you must update the new head node using the update_head(node) function (especially in the case where n = 1). The tail’s next node will be None.

Example:

Input:

reverse_at_position([1, 2, 3, 4, 5, 6], 2)

Output:

[1, 2, 3, 6, 5, 4]
.
.
.
.
.


Comments

Loading comments