Created
April 9, 2017 11:46
-
-
Save linnykoleh/b67b24bdaf6050d653968a0969f8f3b3 to your computer and use it in GitHub Desktop.
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 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