Skip to content

Instantly share code, notes, and snippets.

@arjunrao87
Created December 21, 2014 15:36
Show Gist options
  • Save arjunrao87/c132fbc4bf9eb471cd2e to your computer and use it in GitHub Desktop.
Save arjunrao87/c132fbc4bf9eb471cd2e to your computer and use it in GitHub Desktop.
Reverse each k elements in LL
public void reverseKLL( Node head, int k ){
Node prev;
Node next;
Node current = head;
int count = 0;
while( current != null ){
Node boundaryNode = getBoundaryNode( current );
while( boundaryNode != null && count < k ){
next = current.getNext();
if( count == 0 ){
prev = boundaryNode;
}
current.setNext( prev );
prev = current;
current = next;
count++;
}
}
}
public Node getBoundaryNode( Node node ){
for( int i = 0; i< k; i ++){
if( boundaryNode != null ){
boundaryNode = boundaryNode.getNext();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment