Created
December 21, 2014 15:36
-
-
Save arjunrao87/c132fbc4bf9eb471cd2e to your computer and use it in GitHub Desktop.
Reverse each k elements in LL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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