1.Description
给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
2.Example
输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]
输入:head = [5], left = 1, right = 1
输出:[5]
3.Solution
1.将要倒置的节点存到栈中然后再弹出。(这里没加哑结点,多讨论了left为1时的情况,可以加上哑结点)
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
Stack<ListNode> stack = new Stack<ListNode>();
ListNode sechead = head;
ListNode reshead = head;
for(int i=1;i<left;i++) {
head = head.next;
}
for(int i=left;i<=right;i++) {
stack.push(head);
head = head.next;
}
for(int i=1;i<left-1;i++) {
sechead = sechead.next;
}
if(left==1){
sechead = new ListNode(0,null);
reshead = sechead;
}
while(!stack.empty()) {
sechead.next = stack.pop();
sechead = sechead.next;
}
sechead.next = head;
if(left==1){
reshead = reshead.next;
}
return reshead;
}
}
2.将链表分成要切的段、切的左边的段和切的右边的段,然后将要切的段进行倒置。注意加上哑结点,这样能少讨论情况。
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
// 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre = dummyNode;
// 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
// 建议写在 for 循环里,语义清晰
for (int i = 0; i < left - 1; i++) {
pre = pre.next;
}
// 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
ListNode rightNode = pre;
for (int i = 0; i < right - left + 1; i++) {
rightNode = rightNode.next;
}
// 第 3 步:切断出一个子链表(截取链表)
ListNode leftNode = pre.next;
ListNode curr = rightNode.next;
// 注意:切断链接
pre.next = null;
rightNode.next = null;
// 第 4 步:同第 206 题,反转链表的子区间
reverseLinkedList(leftNode);
// 第 5 步:接回到原来的链表中
pre.next = rightNode;
leftNode.next = curr;
return dummyNode.next;
}
private void reverseLinkedList(ListNode head) {
// 也可以使用递归反转一个链表
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
}
}
3.直接在原链表上倒置,不断将最后的元素放到开头。
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
// 设置 dummyNode 是这一类问题的一般做法
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre = dummyNode;
for (int i = 0; i < left - 1; i++) {
pre = pre.next;
}
ListNode cur = pre.next;
ListNode next;
for (int i = 0; i < right - left; i++) {
next = cur.next;
cur.next = next.next;
next.next = pre.next;
pre.next = next;
}
return dummyNode.next;
}
}