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.
deflinked_list_reverse_k(h:ListNode?,k:float=inf)->(ListNode?,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) """d,t=ListNode(),hwhilehandk>0d.next,h.next,h=h,d.next,h.nextk-=1iftt.next=hd.next,t
43444546474849505152535455565758596061
deflinked_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) """d,t=ListNode(),hwhilehandk>0:d.next,h.next,h=h,d.next,h.nextk-=1ift:t.next=hreturnd.next,t