`

Leetcode - Reverse Nodes in k-Group

 
阅读更多
[分析]
使用递归会Memory Limit Exceed,迭代方式参考
https://leetcode.com/discuss/17483/share-my-java-solution-with-comments-in-line
解析见代码注释
Reverse Linked List, Reverse Linked List II 可使用此题的解题技巧,可联系着体会,而Reorder List中包含Reverse Linked List步骤。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    // prev->head->...->...->tail->|...| //init state of reversing k group [head, tail]
    // prev->tmp->...->...->tail->|...|
    // prev->...->...->tail->tmp->|...| //delete tmp & insert at tail.next
    // prev->...->tail->tmp'->...->head->|...| //keep doing until prev.next = tail
    // prev->tail->...->...->head->|...| // finish state
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null || k < 2)
            return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy, tail = head, tmp;
        while (true) {
            // check remain length of list
            int count = 0;
            while (tail != null) {
                count++;
                if (count == k) break;
                tail = tail.next;
            }
            if (tail == null) break;
            // reverse a K group
            head = prev.next;
            while (prev.next != tail) {
                tmp = prev.next;
                prev.next = tmp.next; // delete tmp from prev.next
                tmp.next = tail.next; // step 1 of inserting tmp at tail.next
                tail.next = tmp; // step 2 of inserting tmp at tail.next
            }
            prev = head;
            tail = head.next;
        }
        return dummy.next;
    }
}

public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode curr = head.next;
        head.next = null;
        ListNode prev = head, next;
        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
    public ListNode reverseList1(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode tail = head;
        while (tail.next != null) {
            tail = tail.next;
        }
        ListNode tmp;
        while (dummy.next != tail) {
            tmp = dummy.next;
            dummy.next = tmp.next;
            tmp.next = tail.next;
            tail.next = tmp;
        }
        return tail;
    }
}


public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        int count = 1;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy; //prev is the (m-1)th node
        while (count < m) {
            prev = prev.next; 
            count++;
        }
        ListNode tail = prev.next;
        while (count < n) { // tail is the nth node
            tail = tail.next;
            count++;
        }
        ListNode tmp;
        while (prev.next != tail) {
            tmp = prev.next;
            prev.next = tmp.next;
            tmp.next = tail.next;
            tail.next = tmp;
        }
        return dummy.next;
    }
}
分享到:
评论

相关推荐

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

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

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

    c c语言_leetcode 0025_reverse_nodes_in_k_group.zip

    leetcode-cpp刷题

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

    _leetcode-python.pdf

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

    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-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刷题手册

    第四章 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最全代码

    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/...

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

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

    lrucacheleetcode-leetcode:leetcode

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

    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