Skip to content

Instantly share code, notes, and snippets.

@linnykoleh
Created April 9, 2017 11:46
Show Gist options
  • Save linnykoleh/b67b24bdaf6050d653968a0969f8f3b3 to your computer and use it in GitHub Desktop.
Save linnykoleh/b67b24bdaf6050d653968a0969f8f3b3 to your computer and use it in GitHub Desktop.
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null)
return null;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
for(int i = 0; i < m-1; i++){
pre = pre.next;
}
ListNode start = pre.next;
ListNode then = start.next;
for(int i=0; i < n-m; i++){
start.next = then.next;
then.next = pre.next;
pre.next = then;
then = start.next;
}
return dummy.next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment