[LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)

https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/javadi-gui-fang-fa-100-by-chadriy-imdgvs6udp/

https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/

描述

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

k 是一个正整数,它的值小于或等于链表的长度。

如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

示例 :

给定这个链表:1->2->3->4->5

当 k = 2 时,应当返回: 2->1->4->3->5

当 k = 3 时,应当返回: 3->2->1->4->5

说明 :

你的算法只能使用常数的额外空间。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

解析

遍历出k个节点,然后翻转子链表,再接到原链表上。

代码

//mypublic ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || k <= 1)
            return head;

        int temp = k;
        ListNode p = head;
        while (temp > 1) {
            if (p == null)
                break;
            p = p.next;
            temp--;
        }
        if (p == null)
            return head;

        ListNode cur_pre = head;
        ListNode next_pre = p.next;
        p.next = null;

        ListNode newHead = reverse(cur_pre);//此时cur_pre.next = null,即cur_pre为新子链表的最后一个节点
        p = cur_pre;
        head = newHead;
        p.next = reverseKGroup(next_pre, k);
        return head;
    }

    public ListNode reverse(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
//注意2个节点的翻转链表即可
public ListNode reverseKGroup(ListNode head, int k) {
        if (k <= 0) return head;
        int temp = k;
        ListNode p = head;
        while (temp > 1) {
            if (p == null) break;
            p = p.next;
            temp--;
        }
        if (p == null) return head;
        ListNode cur_pre = head;
        ListNode pNext_pre = p.next;
        ListNode newHead = reverseList(head, p);
        head = newHead;
        p = cur_pre;
        p.next = reverseKGroup(pNext_pre, k);
        return head;
    }

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

原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/11843396.html

时间: 2024-08-23 19:23:11

[LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)的相关文章

[leetcode] 25. k个一组翻转链表

25. k个一组翻转链表 仍然是链表处理问题,略微复杂一点,边界条件得想清楚,画画图就会比较明确了. reverse函数表示从 front.next节点开始,一共k个节点做反转. 即: 1>2>3>4>5 ,k = 2.当front为1时, 执行reverse后: 1>3>2>4>5 同上个题一样,申请一个空的(无用的)头指针指向第一个节点,会方便许多. public ListNode reverseKGroup(ListNode head, int k)

25. k个一组翻转链表-LeetCode

心得:反转链表加强版,加头节点简化操作,然后写一个方法调用 反转链表,注意next的操作,边界条件!! 代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode reverse

25. K 个一组翻转链表

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度. 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 来源:力扣(LeetCode)链接:https://leetcode-cn.com

25. k个一组翻转链表

题目描述: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 说明 : 你的算法只能使用常数的额外空间. 你不能只是单纯的改变节点内部的值,

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

k个一组翻转链表(java实现)

题目: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 说明 : 你的算法只能使用常数的额外空间. 你不能只是单纯的改变节点内部的值,而是

[LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod