reverse K nodes list

private static Node reverseList(Node head, int k) { Node start = head; Node end = start; int count = 1; while (count++ < k) { // less than K nodes if (end == null) break; end = end.next; } // less than K nodes if (end == null) { return start; } // reverse start/end section return reverse(start, end, reverseList(end.next, k)); } private static Node reverse(Node start, Node end, Node nextListHeader) { if (start == null) return null; Node current = start; Node next = current.next; while (current != end) { Node temp = next.next; next.next = current; current = next; next = temp; } start.next = nextListHeader; return end; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.