题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
利用栈stack把链表数据置换
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
public ListNode reverseBetween (ListNode head, int m, int n) {
Stack stack = new Stack();
ListNode temp = null;
ListNode p = head;
int size = 0;
while (head != null) {
size++;
if (size >= m && size <= n) {
stack.push(head.val);
}
head = head.next;
}
head = p;
size = 0;
while (head != null) {
size++;
if (size >= m && size <= n) {
head.val = stack.pop();
}
head = head.next;
}
return p;
}
}