return new_head def reverse_next_K(l1, k): node = l1 head = node n1 = node.next n2 = n1.next node.next = None for i in range(k - 1): print('node:', node.val) n1.next = node if n2 == None: return head, n1 node = n1 n1 = n2 n2 = n2.next head.next = n1 return head, node a = [k for k in range(1, 6)] l1 = LinkedList(a) l1.print() l = l1.start s = reverseKGroup(l, 3) print('s:') s.print()
import numpy as np from _LinkedList import Node, LinkedList def reverse_LinkedList(ll): node1 = ll.start node2 = ll.start.next node1.next = None while node2 != None: node3 = node2.next node2.next = node1 node1 = node2 node2 = node3 return LinkedList(node1) l = np.random.randint(8, size=20) ll1 = LinkedList(l) ll1.print() ll2 = reverse_LinkedList(ll1) ll2.print()