【模板总结】链表翻转系列

这篇博客介绍了多种链表操作的方法,包括206题的链表反转,24题的两两交换链表中的节点,92题的反转链表II,25题的K个一组翻转链表,以及一个额外的题目,即在不足K个节点时也进行翻转。这些解决方案展示了如何高效地操作链表结构,涉及到了链表的反转、交换和分组操作等核心概念。
摘要由CSDN通过智能技术生成

206. 反转链表

   public ListNode reverseList(ListNode head) {
      ListNode cur = head;
      ListNode pre = null;
      while (cur != null){
         ListNode next = cur.next;
         cur.next = pre;
         pre = cur;
         cur = next;
      }
      return pre;
   }

24. 两两交换链表中的节点

在这里插入图片描述

    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;
        while(cur.next != null && cur.next.next != null) {
        ListNode slow = cur.next;
        ListNode fast = cur.next.next;
        cur.next = fast;
        slow.next = fast.next;
        fast.next = slow;
        cur = slow;
        }
        return dummy.next;
    }

92. 反转链表 II

在这里插入图片描述

   public ListNode reverseBetween(ListNode head, int left, int right) {
      ListNode dummyNode = new ListNode(-1);
      dummyNode.next = head;
      ListNode pre = dummyNode;
      for (int i = 0; i < left - 1; i++) {
         pre = pre.next;
      }
      ListNode rightNode = pre;
      for (int i = 0; i < right - left + 1; i++) {
         rightNode = rightNode.next;
      }
      ListNode leftNode = pre.next;
      ListNode curr = rightNode.next;
      rightNode.next = null;
      
      pre.next = reverse(leftNode);
      // 接回到原来的链表中
      pre.next = rightNode;
      leftNode.next = curr;
      return dummyNode.next;
   }

   private ListNode reverse(ListNode head) {
      // 也可以使用递归反转一个链表
      ListNode pre = null;
      ListNode cur = head;
      while (cur != null) {
         ListNode next = cur.next;
         cur.next = pre;
         pre = cur;
         cur = next;
      }
      return pre;
   }

25. K 个一组翻转链表

   public ListNode reverseKGroup(ListNode head, int k) {
      ListNode dummy = new ListNode(-1);
      dummy.next = head;
      ListNode pre = dummy;
      ListNode end = dummy;
      while(end != null){
         for (int i = 0; i < k && end != null; i++) {
            end = end.next;
         }
         if(end == null){
            
            break;
         }
         ListNode next = end.next;
         ListNode start = pre.next;
         end.next = null;
         pre.next = reverse(start);
         start.next = next;
         pre = start;
         end = start;
      }
      return dummy.next;
   }

   private ListNode reverse(ListNode head) {
      ListNode pre = null;
      ListNode cur = head;
      while (cur != null){
         ListNode next = cur.next;
         cur.next = pre;
         pre = cur;
         cur = next;
      }
      return pre;
   }

另外一个类似的题目是 最后不足K个的时候也要翻转

   //每K个翻转, 最后不足K也要翻转==================
   public ListNode reverseKGroupnotK(ListNode head, int k) {
      ListNode dummy = new ListNode(-1);
      dummy.next = head;
      ListNode pre = dummy;
      ListNode end = dummy;
      while (end != null) {
         for (int i = 0; i < k && end != null; i++) {
            end = end.next;
         }
         if (end == null) {
            ListNode start = pre.next;
            pre.next = reverse(start);
            break;
         }
         ListNode next = end.next;
         ListNode start = pre.next;
         end.next = null;
         pre.next = reverse(start);
         start.next = next;
         pre = start;
         end = start;
      }
      return dummy.next;
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值