链表的旋转与反转 61、24、206、92、25(1简3中1难)

61. 旋转链表(中等)

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

解法一、闭合为环

应该是栈的开销

class Solution {
    public static ListNode rotateRight(ListNode head, int k) {
        if(head == null)return head;
        ListNode i = head;
        int sum = 1;
        Deque<ListNode> st = new ArrayDeque<>();
        //遍历一遍,把所有的按顺序入栈
        while(i.next != null){
            st.push(i);
            i = i.next;
            sum++;
        }
        st.push(i);
        //链接首尾
        i.next = head;
        //取出新的首节点
        k = k % sum+1;
        while(k!=0){
            i = st.pop();
            k--;
        }
        //记录首节点,切断环
        ListNode res = i.next;
        i.next = null;
        return res;
    }
}

解法二 分别翻转

  1. 全反转
  2. 前k反转
  3. 后n-k反转

 和一个字符串反转问题是一样的

24. 两两交换链表中的节点(中等)

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

解法一、模拟

如,对一个链表①→②→③→④→⑤,添加哑巴节点〇

先分别记录123的位置,交换1和2,变成〇→②→①→③→④→⑤结束后把i跳到①处。一次交换涉及四个元素,第二和第三交换,i是第一个,把第二和第四连接。

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null)return head;
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode i = dummy;
        while(i != null && i.next != null && i.next.next != null){//i不是空,i的下一个也不是空
            ListNode one = i.next;
            ListNode two = i.next.next;
            ListNode three = i.next.next.next;
            one.next = three;
            i.next = two;
            two.next = one;
            i = one;
        }
        return dummy.next;
    }
}


206. 反转链表(简单)

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

解法一、 栈+双遍历

扫两遍,第一遍push,第二遍pop,换值

class Solution {
    public ListNode reverseList(ListNode head) {
        Deque<Integer> st = new ArrayDeque<>();
        ListNode n = head;
        while(n != null){
            st.push(n.val);
            n = n.next;
        }
        n = head;
        while(n!=null){
            n.val = st.pop();
            n = n.next;
        }
        return head;
    }
}

解法二、迭代

存值然后换

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/reverse-linked-list/solutions/551596/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

解法三、递归

newHead一路传到结尾,对每一个head,head.next是原有的下一个节点,把它指向head,调转。此时head和head.next相互指向,所以把head.next设成null即可,不设的话会在第一和第二个结点处成环。

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}

 
92. 反转链表 II(中等)

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

解法一、递归

这种题不太会递归,所以按206的解法学着写了一遍。果然应了那句老话。链表的题里,只要舍得用变量就没问题。。 

class Solution {
    public static ListNode reverseBetween(ListNode head, int left, int right) {
        int t = 0;
        ListNode dummy = new ListNode();
        ListNode i = dummy;
        ListNode leftNode = null;
        ListNode rightNode = null;
        dummy.next = head;
        while(t != right){
            if(t == left - 1)leftNode = i;
            i = i.next;
            t++;
        }
        ListNode temp = leftNode.next;
        rightNode = i.next;
        leftNode.next = reverse(leftNode.next,right - left);
        temp.next = rightNode;
        return dummy.next;
    }
    public static ListNode reverse(ListNode head,int count){
        if(count == 0)return head;
        ListNode newNode = di(head.next,count-1);
        head.next.next = head;
        head.next = null;
        return newNode;
    }
}

解法二、穿针引线

对于①→②→③→④→⑤ 2-4,类似冒泡排序,从②开始,读到③,插②前面;读到④,插③前面;连接五。

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;
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/reverse-linked-list-ii/solutions/634701/fan-zhuan-lian-biao-ii-by-leetcode-solut-teyq/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

25. K 个一组翻转链表(困难)

给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。

k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

解法一、栈

start是区块前一个,end是区块后一个。先入栈,遇到cur==null则是剩余节点凑不够一个k,则直接return。凑齐的话出栈串起来

class Solution {
    public static ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy = new ListNode(),cur = head,start,end;
        dummy.next = head;
        start = dummy;
        Deque<ListNode> st = new ArrayDeque<>();
        while(cur.next != null){
            //start是第一个
            cur = start.next;
            for(int i = 0;i < k;i++){
                if(cur == null)return dummy.next;
                st.push(cur);
                cur = cur.next;
            }
            end = cur;
            cur = start;
            for(int i = 0;i < k;i++){
                cur.next = st.pop();
                cur = cur.next;
            }
            cur.next = end;
            start = cur;
        }
        return dummy.next;
    }
}

解法二、递归

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode tail = head;
        int idx = 1;
        //确定尾巴和个数够
        while (tail.next != null && idx < k) {
            tail = tail.next;
            idx++;
        }
        if (idx < k) {
            return head;
        }
        //确定区间右侧第一个,把区间最后一个的next置为零,这里是reverseListNode的判断条件
        ListNode nxt = tail.next;
        tail.next = null;
        //找到第一段的最后一个,也就是链表的头
        ListNode newHead = reverseListNode(head);
        //把第一段之后的重新翻转
        ListNode nextNode = reverseKGroup(nxt, k);
        //把头(此时是第一段的尾)和下一段的头链接
        head.next = nextNode;
        return newHead;
    }

    public ListNode reverseListNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode nxt = head.next;
        ListNode newHead = reverseListNode(nxt);
        nxt.next = head;
        head.next = null;
        return newHead;
    }
}

解法三、模拟

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode hair = new ListNode(0);
        hair.next = head;
        ListNode pre = hair;

        while (head != null) {
            ListNode tail = pre;
            // 查看剩余部分长度是否大于等于 k
            for (int i = 0; i < k; ++i) {
                tail = tail.next;
                if (tail == null) {
                    return hair.next;
                }
            }
            ListNode nex = tail.next;
            ListNode[] reverse = myReverse(head, tail);
            head = reverse[0];
            tail = reverse[1];
            // 把子链表重新接回原链表
            pre.next = head;
            tail.next = nex;
            pre = tail;
            head = tail.next;
        }

        return hair.next;
    }

    public ListNode[] myReverse(ListNode head, ListNode tail) {
        ListNode prev = tail.next;
        ListNode p = head;
        while (prev != tail) {
            ListNode nex = p.next;
            p.next = prev;
            prev = p;
            p = nex;
        }
        return new ListNode[]{tail, head};
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/reverse-nodes-in-k-group/solutions/248591/k-ge-yi-zu-fan-zhuan-lian-biao-by-leetcode-solutio/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

碎碎念

  • 更熟递归了。。栈效率真的有点慢24/206/92/25本质上在考同一个东西。穿针引线的方法真的很巧妙有趣
  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值