Created
July 10, 2019 16:39
-
-
Save Desolve/5f1c1300afeb8aa6ab4cfabebbb09f28 to your computer and use it in GitHub Desktop.
0092 Reverse Linked List II
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 singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { | |
public ListNode reverseBetween(ListNode head, int m, int n) { | |
if (m == n) return head; | |
Stack<ListNode> st = new Stack<>(); | |
ListNode left = null; | |
ListNode node = head; | |
if (m != 1) { | |
for (int i = 1; i < m - 1; ++i) node = node.next; | |
left = node; | |
node = node.next; // position m | |
} | |
for (int i = m; i <= n; ++i) { | |
st.push(node); | |
node = node.next; | |
} | |
ListNode l1 = st.pop(), l2 = null; | |
if (m == 1) | |
head = l1; | |
else | |
left.next = l1; | |
while (!st.isEmpty()) { | |
l2 = st.pop(); | |
l1.next = l2; | |
l1 = l2; | |
} | |
l1.next = node; | |
return head; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment