Given a linked list and a number k. You have to reverse first part of linked list with k nodes and the second part with n-k nodes.
Example 1:
Input: 1 -> 2 -> 3 -> 4 -> 5
k = 2
Output: 2 -> 1 -> 5 -> 4 -> 3
Explanation: As k = 2 , so the first part 2
nodes: 1 -> 2 and the second part with 3 nodes:
3 -> 4 -> 5. Now after reversing the first part: 
2 -> 1 and the second part: 5 -> 4 -> 3.
So the output is: 2 -> 1 -> 5 -> 4 -> 3
Example 2:
Input: 1 -> 2 -> 4 -> 3
k = 3
Output: 4 -> 2 -> 1 -> 3
Explanation: As k = 3 , so the first part 
3 nodes: 4 -> 2 -> 1 and the second part
with 1 nodes: 3. Now after reversing the 
first part: 1 -> 2 -> 4 and the 
second part: 3. So the output is: 1 -> 2 -> 4 -> 3
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function reverse() which takes head node of the linked list and a integer k as input parameters and returns head node of the linked list after reversing both parts. 
Constraints:
1 <= N <= 10^{5 }
1 <= k < N^{ }
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:


from typing import Optional



"""



Definition for singly Link List Node

class Node:

    def __init__(self,x):

        self.data=x

        self.next=None



You can also use the following for printing the link list.

printList(node)

"""



class Solution:

    def reverse(self, head : Optional['Node'], k : int) -> Optional['Node']:

        # code here

        



Solutions:
from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		a = head
		list1 = []
		while a is not None:
			list1.append(a.data)
			a = a.next
		list2 = list1[:k][::-1] + list1[k:][::-1]
		newnode = None
		head1 = None
		for i in list2:
			m = Node(i)
			if head1 is None:
				head1 = m
				newnode = head1
			else:
				newnode.next = m
				newnode = newnode.next
		return head1


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		n = 0
		temp = head
		while temp:
			n += 1
			temp = temp.next
		prev = None
		cur = head
		while k:
			temp = cur.next
			cur.next = prev
			prev = cur
			cur = temp
			k -= 1
		next2 = None
		while cur:
			temp = cur.next
			cur.next = next2
			next2 = cur
			cur = temp
		dummy = prev
		while dummy.next != None:
			dummy = dummy.next
		dummy.next = next2
		return prev


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		c = k
		tmp = head
		pre = None
		while head and c:
			nxt = head.next
			head.next = pre
			pre = head
			head = nxt
			c -= 1
		temp = pre
		pre = None
		while head:
			nxt = head.next
			head.next = pre
			pre = head
			head = nxt
		tmp.next = pre
		return temp


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = Node(-1)
		temp.next = head
		prev = None
		while k > 1:
			tempo = head.next
			head.next = prev
			prev = head
			head = tempo
			k -= 1
		curr = head.next
		head.next = prev
		prev2 = None
		while curr.next:
			tempo_ = curr.next
			curr.next = prev2
			prev2 = curr
			curr = tempo_
		curr.next = prev2
		temp.next.next = curr
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		prev = None
		i = 0
		while head:
			if i == k:
				newhead = prev
			last = head
			nxt = head.next
			if i == k:
				head.next = None
			else:
				head.next = prev
			prev = head
			head = nxt
			i += 1
		temp.next = prev
		return newhead


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = head
		for _ in range(k):
			prev = prev.next
		curr = head
		for _ in range(k):
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		head = prev
		curr = head
		dummy = None
		for _ in range(k):
			dummy = curr
			curr = curr.next
		prev = None
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		dummy.next = prev
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		current = head
		while k and current is not None:
			next = current.next
			current.next = prev
			prev = current
			current = next
			k -= 1
		prev1 = None
		while current is not None:
			next = current.next
			current.next = prev1
			prev1 = current
			current = next
		totell = prev
		while prev:
			if prev.next == None:
				prev.next = prev1
				break
			prev = prev.next
		head = totell
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		n = k
		prev = head
		for i in range(k):
			prev = prev.next
		curr = head
		while n > 0:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
			n -= 1
		head = prev
		curr = head
		first_k = head
		for _ in range(k - 1):
			curr = curr.next
			first_k = first_k.next
		prev = None
		curr = curr.next
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		first_k.next = prev
		return head


from typing import Optional

class Solution:

	def linkRev(self, head):
		temp = head
		prev = None
		curr = head
		next = None
		while curr != None:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		head = prev
		return (head, temp)

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		for i in range(k - 1):
			temp = temp.next
		Head2 = temp.next
		temp.next = None
		link1 = self.linkRev(head)
		link2 = self.linkRev(Head2)
		link1[1].next = link2[0]
		return link1[0]


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if head.next is None:
			return head
		orig_head = head
		tail = head
		count = 1
		while head.next is not None:
			if count == k:
				final_head = head
				final_tail = head.next
			count += 1
			new_head = head.next
			head.next = tail
			tail = head
			head = new_head
		head.next = tail
		orig_head.next = head
		final_tail.next = None
		return final_head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		cur = head
		c = 0
		while cur and c != k:
			next = cur.next
			cur.next = prev
			prev = cur
			cur = next
			c += 1
		h = prev
		while h.next:
			h = h.next
		c = cur
		h.next = None
		l = None
		while c:
			n = c.next
			c.next = l
			l = c
			c = n
		h.next = l
		return prev


from typing import Optional

class Solution:

	def reverse_list(self, head):
		prev = None
		curr = head
		next = None
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		return prev

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		curr = head
		for i in range(k):
			prev = curr
			curr = curr.next
		prev.next = None
		head1 = head
		head2 = curr
		head1 = self.reverse_list(head1)
		head2 = self.reverse_list(head2)
		aux = head1
		while aux.next:
			aux = aux.next
		aux.next = head2
		return head1


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:

		def reverseNode(head):
			curr = head
			prev = None
			while curr != None:
				nxt = curr.next
				curr.next = prev
				prev = curr
				curr = nxt
			return prev
		temp = head
		count = 1
		while count < k:
			temp = temp.next
			count += 1
		secondNode = reverseNode(temp.next)
		temp.next = None
		firstNode = reverseNode(head)
		temp = firstNode
		while temp.next != None:
			temp = temp.next
		temp.next = secondNode
		return firstNode


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		while k > 1 and temp != None:
			temp = temp.next
			k -= 1
		second = self.rev(temp.next)
		if temp:
			temp.next = None
		first = self.rev(head)
		temp = first
		while temp.next != None:
			temp = temp.next
		temp.next = second
		return first

	def rev(self, head):
		prev = None
		curr = head
		nxt = None
		while curr != None:
			nxt = curr.next
			curr.next = prev
			prev = curr
			curr = nxt
		return prev


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		cur = head
		d = Node(0)
		while k > 0:
			(t, cur) = (cur, cur.next)
			(d.next, t.next) = (t, d.next)
			k -= 1
		while cur:
			(t, cur) = (cur, cur.next)
			(head.next, t.next) = (t, head.next)
		return d.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		x = []
		while head:
			x.append(head.data)
			head = head.next
		p = []
		p[:] = x[0:k][::-1] + x[k:][::-1]
		c = Node(p[0])
		cr = c
		for i in range(1, len(p)):
			y = Node(p[i])
			cr.next = y
			cr = y
		return c


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		temp = Node(-99)
		temp.next = head
		while k > 1:
			temp1 = head.next
			head.next = prev
			prev = head
			head = temp1
			k -= 1
		curr = head.next
		head.next = prev
		prev2 = None
		while curr.next:
			temp2 = curr.next
			curr.next = prev2
			prev2 = curr
			curr = temp2
		curr.next = prev2
		temp.next.next = curr
		return head


from typing import Optional

class Solution:

	def rev_list(self, l):
		prev = None
		while l.next is not None:
			next_node = l.next
			l.next = prev
			prev = l
			l = next_node
		l.next = prev
		return l

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		h1 = head
		h2 = None
		while k >= 2:
			if head.next is None:
				return h1
			head = head.next
			k -= 1
		h2 = head.next
		head.next = None
		h1 = self.rev_list(h1)
		if h2 is not None:
			h2 = self.rev_list(h2)
		head = h1
		while h1.next != None:
			h1 = h1.next
		h1.next = h2
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		i = 0
		current = head
		dummy = None
		upcomming = dummy
		while current and i < k:
			i += 1
			upcomming = current.next
			current.next = dummy
			dummy = current
			current = upcomming
		dummy2 = None
		current = current
		upcomming = dummy2
		while current:
			upcomming = current.next
			current.next = dummy2
			dummy2 = current
			current = upcomming
		head.next = dummy2
		return dummy


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		cur_nxt = head
		cnt_nxt = 0
		while cnt_nxt != k:
			cur_nxt = cur_nxt.next
			cnt_nxt += 1
		cur = head
		cnt = 0
		prev = None
		while cnt != k and cur:
			cnt += 1
			nxt = cur.next
			cur.next = prev
			prev = cur
			cur = nxt
		prev_next = None
		while cur_nxt:
			nxt = cur_nxt.next
			cur_nxt.next = prev_next
			prev_next = cur_nxt
			cur_nxt = nxt
		head.next = prev_next
		return prev


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		curr = head
		prev = None
		new_head = None
		while curr != None:
			k = k - 1
			if k < 0 and new_head == None:
				new_head = prev
				prev = None
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		head.next = prev
		return new_head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		curr = head
		prev = None
		while k > 0:
			nex = curr.next
			curr.next = prev
			prev = curr
			curr = nex
			k -= 1
		prev2 = None
		x = prev
		while curr != None:
			nex = curr.next
			curr.next = prev2
			prev2 = curr
			curr = nex
		while prev.next != None:
			prev = prev.next
		prev.next = prev2
		return x


from typing import Optional

class Solution:

	def rll(self, root):
		prev = None
		curr = root
		temp = None
		while curr != None:
			temp = curr.next
			curr.next = prev
			prev = curr
			curr = temp
		return prev

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		node = head
		while k > 1:
			node = node.next
			k -= 1
		sec = self.rll(node.next)
		node.next = None
		first = self.rll(head)
		head.next = sec
		return first


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		rev = None
		rev2 = None
		temp = head
		n = None
		while temp != None:
			if k > 0:
				dump = rev
				nn = Node(temp.data)
				rev = nn
				rev.next = dump
				if rev.next == None:
					n = rev
				k = k - 1
			else:
				dump2 = rev2
				nn1 = Node(temp.data)
				rev2 = nn1
				rev2.next = dump2
			temp = temp.next
		n.next = rev2
		return rev


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		s = list()
		n = head
		while n.next is not None:
			s.append(str(n.data))
			n = n.next
		s.append(str(n.data))
		result = list()
		for n in range(0, k):
			result.append(s[k - n - 1])
		for n in range(0, len(s) - k):
			result.append(s[len(s) - n - 1])
		r = Node(result[len(result) - 1])
		for i in range(0, len(result) - 2):
			n = Node(result[len(result) - 2 - i])
			n.next = r
			r = n
		n = Node(result[0])
		n.next = r
		r = n
		return r


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		temp = head
		node = None
		while temp:
			nxt = temp.next
			temp.next = prev
			prev = temp
			temp = nxt
			k -= 1
			if k == 0:
				node = temp
		head.next = prev
		head = node.next
		node.next = None
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		l = []
		p = head
		while p:
			l.append(p.data)
			p = p.next
		l = l[:k][::-1] + l[:k - 1:-1]
		head = Node(0)
		curr = head
		for i in l:
			curr.next = Node(i)
			curr = curr.next
		return head.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		count = 0
		prev = None
		current = head
		count = 0
		while count < k:
			next = current.next
			current.next = prev
			prev = current
			current = next
			count += 1
		second_prev = head
		while count < k - 1:
			second_prev = second_prev.next
			count += 1
		break_prev = None
		while current is not None:
			next = current.next
			current.next = break_prev
			break_prev = current
			current = next
		second_prev.next = break_prev
		head = prev
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		total = 0
		back = None
		var = head
		while head != None:
			front = head.next
			head.next = back
			back = head
			head = front
			total += 1
		i = 1
		front = back
		while i < total - k:
			back = back.next
			i += 1
		head = back.next
		back.next = None
		var.next = front
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if not head:
			return
		prev = None
		cur = head
		for _ in range(k):
			next = cur.next
			cur.next = prev
			prev = cur
			cur = next
		cur = next
		new_head = prev
		prev = None
		while cur:
			next = cur.next
			cur.next = prev
			prev = cur
			cur = next
		head.next = prev
		return new_head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		count = 0
		head1 = head
		head2 = None
		curr = head
		prev = None
		while count < k and curr.next:
			count += 1
			head2 = curr.next
			prev = curr
			curr = curr.next
		prev.next = None
		curr = head1
		last = curr
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		head = prev
		if head2:
			prev = None
			curr = head2
			while curr:
				next = curr.next
				curr.next = prev
				prev = curr
				curr = next
			head2 = prev
		last.next = head2
		return head


from typing import Optional
import sys
sys.setrecursionlimit(100000)

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if head == None:
			return
		curr1 = head
		count = 1
		while curr1.next != None:
			count = count + 1
			curr1 = curr1.next
		if k > count:
			return head
		prev = None
		curr = head
		while k != 0:
			prev = curr
			curr = curr.next
			k = k - 1
		prev.next = None
		head1 = self.reverse_helper(head)
		head2 = self.reverse_helper(curr)
		curr2 = head1
		while curr2.next != None:
			curr2 = curr2.next
		curr2.next = head2
		return head1

	def reverse_helper(self, head):
		if head == None:
			return None
		if head.next == None:
			return head
		smallhead = self.reverse_helper(head.next)
		tail = head.next
		tail.next = head
		head.next = None
		return smallhead


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		ar = []
		p = head
		while head:
			ar.append(head.data)
			head = head.next
		c = 0
		ar = list(reversed(ar[:k])) + list(reversed(ar[k:]))
		ko = p
		while p:
			p.data = ar[c]
			c += 1
			p = p.next
		return ko


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		cnt = 0
		prev = None
		cur = head
		suc = None
		toadd = head
		for i in range(k):
			suc = cur.next
			cur.next = prev
			prev = cur
			cur = suc
		ans = prev
		prev = None
		suc = None
		while cur:
			suc = cur.next
			cur.next = prev
			prev = cur
			cur = suc
		toadd.next = prev
		return ans


from typing import Optional
from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		leng = 0
		a = head
		while a:
			leng += 1
			a = a.next
		dummy = Node(-1)
		b = dummy
		for i in range(k):
			dummy.next = head
			head = head.next
			dummy = dummy.next
		dummy.next = None
		(curr, prev) = (b.next, None)
		while curr:
			temp = curr.next
			curr.next = prev
			prev = curr
			curr = temp
		b.next = prev
		dummy1 = Node(-1)
		c = dummy1
		for i in range(leng - k):
			dummy1.next = head
			head = head.next
			dummy1 = dummy1.next
		dummy1.next = None
		(curr1, prev1) = (c.next, None)
		while curr1:
			temp1 = curr1.next
			curr1.next = prev1
			prev1 = curr1
			curr1 = temp1
		c.next = prev1
		dummy = Node(-1)
		res = dummy
		while prev:
			dummy.next = prev
			prev = prev.next
			dummy = dummy.next
		while prev1:
			dummy.next = prev1
			prev1 = prev1.next
			dummy = dummy.next
		return res.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if head == [] or head == None:
			return head
		l = []
		current = head
		while current:
			l.append(current.data)
			current = current.next
		l1 = l[:k][::-1]
		l2 = l[k:][::-1]
		l1.extend(l2)
		dumy_node = dumy_head = Node(0)
		for each in l1:
			dumy_node.next = Node(each)
			dumy_node = dumy_node.next
		return dumy_head.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		curr = head
		nex = head.next
		join = head
		count = 0
		while count != k:
			curr.next = prev
			prev = curr
			curr = nex
			if curr.next != None:
				nex = curr.next
				count += 1
		h = prev
		prev = None
		while curr.next != None:
			curr.next = prev
			prev = curr
			curr = nex
			if curr.next != None:
				nex = curr.next
		curr.next = prev
		join.next = curr
		return h


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		a = []
		head1 = head
		if head1.data != None and head1.next != None:
			while head1 != None:
				a.append(head1.data)
				head1 = head1.next
			b = a[k - 1::-1]
			c = a[:k - 1:-1]
			d = b + c
			head1 = head
			i = 0
			while i < len(d):
				head1.data = d[i]
				i = i + 1
				head1 = head1.next
			return head
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		(pre, cur) = (None, head)
		while k:
			t = cur.next
			cur.next = pre
			pre = cur
			cur = t
			k -= 1
		(sp, sc) = (None, cur)
		while sc:
			t = sc.next
			sc.next = sp
			sp = sc
			sc = t
		head.next = sp
		return pre


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = []
		while head:
			temp.append(head.data)
			head = head.next
		fi = temp[:k]
		la = temp[k:]
		temp = fi[::-1] + la[::-1]
		ans = dum = Node(0)
		x = len(temp)
		while x > 0:
			dum.next = Node(temp.pop(0))
			dum = dum.next
			x -= 1
		return ans.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		(pre, cur, res) = (None, head, head)
		i = 0
		while i < k or cur:
			if i == k:
				res = pre
				pre = None
			t = cur.next
			cur.next = pre
			pre = cur
			cur = t
			i += 1
		head.next = pre
		return res


from typing import Optional

class Solution:

	def reverse2(self, head):
		prev = None
		cur = head
		while cur is not None:
			next = cur.next
			cur.next = prev
			prev = cur
			cur = next
		return prev

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if head == None or head.next == None:
			return head
		count = 0
		n = None
		p = None
		temp = head
		while temp.next != None and count <= k - 1:
			p = temp
			temp = temp.next
			count += 1
		p.next = None
		h1 = self.reverse2(head)
		h2 = self.reverse2(temp)
		head.next = h2
		return h1


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:

		def rev(adr):
			prev = None
			Next = None
			curr = adr
			while curr:
				Next = curr.next
				curr.next = prev
				prev = curr
				curr = Next
			return prev
		curr = head
		i = 1
		while i != k:
			curr = curr.next
			i += 1
		nadd = curr.next
		curr.next = None
		head = rev(head)
		nexad = rev(nadd)
		curr1 = head
		while curr1.next != None:
			curr1 = curr1.next
		curr1.next = nexad
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		prev = None
		i = 0
		while i != k:
			newnode = temp.next
			temp.next = prev
			prev = temp
			temp = newnode
			i += 1
		pre = None
		while temp:
			newnode = temp.next
			temp.next = pre
			pre = temp
			temp = newnode
		temp = prev
		while temp.next != None:
			temp = temp.next
		temp.next = pre
		node = prev
		while node != None:
			print(node.data, end=' ')
			node = node.next


from typing import Optional
from sys import stdin, setrecursionlimit
import queue
setrecursionlimit(10 ** 9)

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		curr = head
		while curr:
			if k > 0:
				ahead = curr.next
				curr.next = prev
				prev = curr
				curr2 = curr
				curr = ahead
				k -= 1
			elif k == 0:
				prev = None
				k -= 1
			else:
				ahead = curr.next
				curr.next = prev
				prev = curr
				curr = ahead
				k -= 1
		head.next = prev
		return curr2


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		i = 0
		first = None
		second = None
		temp = dummy = head
		initial = None
		while i < k and dummy:
			temp = temp.next
			dummy.next = first
			first = dummy
			dummy = temp
			if i == 0:
				initial = first
			i += 1
		while dummy:
			temp = temp.next
			dummy.next = second
			second = dummy
			dummy = temp
		initial.next = second
		return first


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		next_node = None
		prev = None
		count = 1
		while k == -1 and temp or count <= k:
			next_node = temp.next
			temp.next = prev
			prev = temp
			temp = next_node
			count += 1
		if k != -1:
			head.next = self.reverse(temp, -1)
		return prev


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		current = head
		a = []
		while current.next != None:
			a.append(current.data)
			current = current.next
		a.append(current.data)
		a = a[k - 1:0:-1] + [a[0]] + a[len(a) - 1:k - 1:-1]
		node = Node(a[0])
		current = node
		for i in a[1:]:
			current.next = Node(i)
			current = current.next
		return node


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		if not head:
			return
		curr1 = head
		for i in range(k - 1):
			curr1 = curr1.next
		curr2 = curr1.next
		curr1.next = None
		curr3 = self.reverse1(head)
		res = curr3
		while curr3.next != None:
			curr3 = curr3.next
		curr4 = self.reverse1(curr2)
		curr3.next = curr4
		return res

	def reverse1(self, head):
		prev = None
		curr = head
		while curr != None:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		return prev


from typing import Optional

class Solution:

	def rev(self, head):
		curr = head
		prev = None
		nex = None
		while curr:
			nex = curr.next
			curr.next = prev
			prev = curr
			curr = nex
		return prev

	def print_l(self, head1):
		while head1:
			print(head1.data)
			head1 = head1.next

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		try:
			head1 = head
			temp = head1
			while k > 1:
				temp = temp.next
				k = k - 1
			head2 = temp.next
			temp.next = None
			head1 = self.rev(head1)
			head2 = self.rev(head2)
			temp = head1
			while temp.next:
				temp = temp.next
			temp.next = head2
			return head1
		except Exception as e:
			print('E', e)


from typing import Optional

class Solution:

	def reversed(self, head):
		cur = head
		prev = None
		while cur != None:
			nxt = cur.next
			cur.next = prev
			prev = cur
			cur = nxt
		return prev

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		for i in range(k - 1):
			temp = temp.next
		secondHead = self.reversed(temp.next)
		temp.next = None
		firstHead = self.reversed(head)
		temp = firstHead
		while temp.next != None:
			temp = temp.next
		temp.next = secondHead
		return firstHead


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		head1 = head
		arr = []
		while head1 is not None:
			arr.append(head1.data)
			head1 = head1.next
		arr1 = arr[:k]
		arr2 = arr[k:]
		arr1 = arr1[::-1]
		arr2 = arr2[::-1]
		arr = arr1 + arr2
		head1 = head
		while head1 is not None:
			head1.data = arr.pop(0)
			head1 = head1.next
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev1 = None
		cur1 = head
		i = 0
		while i < k:
			nxtnode = cur1.next
			cur1.next = prev1
			prev1 = cur1
			cur1 = nxtnode
			i += 1
		cur2 = cur1
		prev2 = None
		while cur2 != None:
			nxtnode = cur2.next
			cur2.next = prev2
			prev2 = cur2
			cur2 = nxtnode
		temp = prev1
		while temp.next != None:
			temp = temp.next
		temp.next = prev2
		return prev1


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		l = []
		s = []
		p = []
		while temp is not None:
			l.append(temp.data)
			temp = temp.next
		for i in range(len(l) - k):
			s.append(l.pop())
		for i in range(len(l)):
			p.append(l.pop())
		s = p + s
		h = Node(0)
		q = h
		for i in range(len(s)):
			h.next = Node(s[i])
			h = h.next
		h.next = None
		return q.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		curr = head
		nxt = curr.next
		strt = head
		for i in range(k):
			curr.next = prev
			prev = curr
			curr = nxt
			head = prev
			nxt = curr.next
		strt.next = curr
		prev = strt
		strt2 = strt.next
		while True:
			curr.next = prev
			prev = curr
			curr = nxt
			if curr == None:
				break
			nxt = curr.next
		strt.next = prev
		strt2.next = None
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		f = []
		r = []
		t = head
		j = 0
		while t:
			j += 1
			if j <= k:
				f.append(t)
			else:
				r.append(t)
			t = t.next
		head = f[-1]
		j = -1
		while j > -len(f):
			f[j].next = f[j - 1]
			j -= 1
		f[0].next = r[-1]
		j = -1
		while j > -len(r):
			r[j].next = r[j - 1]
			j -= 1
		r[0].next = None
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		curr = head
		prev = None
		count = 0
		while count != k:
			newNode = curr.next
			curr.next = prev
			prev = curr
			curr = newNode
			count += 1
		pre = None
		while curr:
			newNode = curr.next
			curr.next = pre
			pre = curr
			curr = newNode
		curr = prev
		while curr.next != None:
			curr = curr.next
		curr.next = pre
		node = prev
		while node != None:
			print(node.data, end=' ')
			node = node.next


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		P1 = head
		count = 1
		while count < k:
			P1 = P1.next
			count += 1
		P2 = P1.next
		P1.next = None

		def reverse_ll(head):
			prev = None
			current = head
			while current is not None:
				next = current.next
				current.next = prev
				prev = current
				current = next
			head = prev
			return head
		P2 = reverse_ll(P2)
		P3 = reverse_ll(head)
		new_head = P3
		while P3.next:
			P3 = P3.next
		P3.next = P2
		return new_head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		temp = head
		prev = None
		while k:
			k -= 1
			_next = temp.next
			temp.next = prev
			prev = temp
			temp = _next
		newHead = prev
		prev = None
		while temp:
			_next = temp.next
			temp.next = prev
			prev = temp
			temp = _next
		head.next = prev
		return newHead


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:

		def reverseList(val):
			cur = val
			pre = None
			while cur:
				nxt = cur.next
				cur.next = pre
				pre = cur
				cur = nxt
			return pre
		temp = head
		while temp:
			k -= 1
			if k == 0:
				store = temp.next
				temp.next = None
				front = reverseList(head)
				second = reverseList(store)
				break
			temp = temp.next
		head = front
		while front.next:
			front = front.next
		front.next = second
		return head


from typing import Optional

class Solution:

	def reverse(self, head: Optional['Node'], k: int) -> Optional['Node']:
		prev = None
		c = head
		n = head
		i = 0
		while n != None and i < k:
			n = n.next
			c.next = prev
			prev = c
			c = n
			i += 1
		temp = None
		c = n
		while n != None:
			n = n.next
			c.next = temp
			temp = c
			c = n
			i += 1
		head.next = temp
		return prev