`

LeetCode 25 - Reverse Nodes in k-Group

 
阅读更多

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 nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

public ListNode reverseKGroup(ListNode head, int k) {
    if(head == null || head.next == null || k<=1) return head;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode node = dummy;
    int i = 0;
    while(head != null) {
        i++;
        if(i%k == 0) {
            node = reverse(node, head.next);
            head = node.next;
        } else {
            head = head.next;
        }
    }

    return dummy.next;
}

private ListNode reverse(ListNode preHead, ListNode end) {
    ListNode tail = preHead.next;
    ListNode walker = tail.next;
    while(walker != end) {
        tail.next = walker.next;
        walker.next = preHead.next;
        preHead.next = walker;
        walker = tail.next;
    }
    return tail;
}

 

分享到:
评论

相关推荐

    c语言-leetcode 0025-reverse-nodes-in-k-group.zip

    c c语言_leetcode 0025_reverse_nodes_in_k_group.zip

    js-leetcode题解之25-reverse-nodes-in-k-group.js

    js js_leetcode题解之25-reverse-nodes-in-k-group.js

    leetcode296-leetcode-in-py-and-go:Go中的Leetcode

    reverse-nodes-in-k-group: 解析 pre_for_next 到辅助函数 29:除以两个整数:溢出; 两反 31:下一个排列:再做一次(排序!) 32:最长有效(),使用栈,左推idx 33: search-in-rotated-sorted-array ,比较中间值...

    _leetcode-python.pdf

    - Swap Nodes in Pairs / Reverse Nodes in k-Group: 这两个问题涉及到在链表中按特定规则交换节点或反转节点组。 - Remove Duplicates from Sorted Array / Remove Element: 删除排序数组中的重复项,或从数组中...

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer ...25. Reverse Nodes in k-Group 26. Remove Dupli

    leetcode-cpp刷题

    - **2.2.9 Reverse Nodes in k-Group** - 每k个一组反转链表。 - 实现思路:维护一个指针指向当前组的前一个节点,依次反转每一组。 - **2.2.10 Copy List with Random Pointer** - 复制一个带随机指针的链表。...

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth ...Nodes in ...k ...Reverse Nodes in k-Group Trapping Rain Water

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest ...Reverse ...25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3

    leetcode添加元素使和等于-leetcode:力码

    leetcode添加元素使和等于 总结 按照类别分类来刷 刷当前题的时候,看下『题目描述』最底下有个『相似题目』,这些题的思路大致也相当 ...reverse-nodes-in-k-group 二叉树 实现一个二叉树 二叉树二叉树的

    Dir-For-LeetCode

    问题 ... 025_Reverse_Nodes_in_k-Group 026_Remove_Duplicates_from_Sorted_Array 027_Remove_Element 028_Implement_strStr() 029_Divide_Two_Integers 030_Substring_with_Concatenation_of

    算法面试通关40讲完整课件 05-07 数组、链表

    5. K 个一组翻转链表(Reverse Nodes in K-Group, 如LeetCode的第25题):将链表中的节点每K个一组进行反转。 解决这些题目时,通常需要掌握以下技巧: - 使用快慢指针(Floyd's Tortoise and Hare)来检测链表环。...

    Amazon近半年电面题.pdf

    20. SwapNodesinPairs: 在链表中交换每两个相邻节点,这需要处理好链表的指针。 21. ReverseNodesink-Group: 以k个节点为一组翻转链表,这是一个在链表上的递归或迭代问题。 22. RemoveElement: 从数组中移除特定...

Global site tag (gtag.js) - Google Analytics