pydsa.linked¶
linked_list_reverse_k ¶
linked_list_reverse_k(
h: Optional[ListNode], k: float = inf
) -> Tuple[Optional[ListNode], Optional[ListNode]]
Reverse the first k nodes of the list at h, in place.
Returns the new head and the new tail. The tail is the node that was h, and its next is the first node that was not reversed. With k left out, or a list shorter than k, the whole list is reversed and the tail's next is None.
head, tail = linked_list_reverse_k(build_linked_list([1, 2, 3, 4, 5]), 3) get_list_values(head), tail.val ([3, 2, 1, 4, 5], 1)
Source code in pydsa/linked.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |