Created
May 15, 2017 22:08
-
-
Save thanlau/74b05d2036c45b5a04dd0d6d5526699c 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
/** | |
* Definition for ListNode | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } | |
*/ | |
public class Solution { | |
/** | |
* @param ListNode head is the head of the linked list | |
* @oaram m and n | |
* @return: The head of the reversed ListNode | |
*/ | |
public ListNode reverseBetween(ListNode head, int m , int n) { | |
if(head == null || m > n){ | |
return head; | |
} | |
ListNode dummy = new ListNode(1); | |
dummy.next = head; | |
head = dummy; | |
for(int i = 1 ; i < m; i++){ | |
head = head.next; | |
} | |
ListNode prev = head; | |
ListNode curr = head.next; | |
ListNode Nnode = curr; | |
ListNode next = curr.next; | |
for(int i = m; i< n; i++){ | |
ListNode temp = next.next; | |
next.next = curr; | |
curr = next; | |
next = temp; | |
} | |
Nnode.next = next; | |
prev.next = curr; | |
return dummy.next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment