LeetCode:Reverse Nodes in k-Group

题目链接: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

题目给出一个单链表,以其中每K个结点为一组,逆置每一个组,并且只能使用常量的内存空间。

第一种思路:原地逆置

public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode preGroupTail = null, currentGroupHead = head, nextGroupHead = null, current = head;
        for (int i=0; current != null; i++) {
            if (( (i+1) % k) == 0) {
                if (i == k-1) {
                    head = current;
                }
                current = current.next;
                nextGroupHead = current;
                reverse(preGroupTail, currentGroupHead, nextGroupHead);
                preGroupTail = currentGroupHead;
                currentGroupHead = nextGroupHead;
                continue;
            }
            current = current.next;
        }
        return head;
    }
    // 逆置K个节点
    public void reverse(ListNode preGroup, ListNode head, ListNode nextGroup) {
        ListNode pre = nextGroup;
        ListNode next = null;
        while (head != nextGroup) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        if (preGroup != null) {
            preGroup.next = pre;
        }
    }
}



第二种思路:使用数据结构的方法

public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null) {
            return head;
        }
        LinkedList<ListNode> list = new LinkedList<>();
        ListNode current = head, present = null;
        head = null;
        while (current != null) {
            int i = 0;
            for (; i<k && current!=null; i++) {
                list.addFirst(current);   // 入栈
                current = current.next;
            }

            if (i < k) {
                if (head == null) {
                    head = list.removeLast();
                    present = head;
                }
                while (list.size() != 0) {
                    present.next = list.removeLast();
                    present = present.next;
                }
            } else {
                if (head == null) {
                    head = list.removeFirst();
                    present = head;
                    present.next = null;
                }
                while (list.size() != 0) {
                    present.next = list.removeFirst();
                    present = present.next;
                    present.next = null;
                }
            }
        }
        return head;
    }
}



第三种思路:recursive(讨论区的解法,思路比较新奇优雅,只是递归方法占用的内存空间比较大)

public ListNode reverseKGroup(ListNode head, int k) {
    ListNode curr = head;
    int count = 0;
    while (curr != null && count != k) { // find the k+1 node
        curr = curr.next;
        count++;
    }
    if (count == k) { // if k+1 node is found
        curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
        // head - head-pointer to direct part, 
        // curr - head-pointer to reversed part;
        while (count-- > 0) { // reverse current k-group: 
            ListNode tmp = head.next; // tmp - next head in direct part
            head.next = curr; // preappending "direct" head to the reversed list 
            curr = head; // move head of reversed part to a new node
            head = tmp; // move "direct" head to the next node in direct part
        }
        head = curr;
    }
    return head;
}



作业部落

posted @ 2019-12-15 09:26  夜读春秋  阅读(111)  评论(0编辑  收藏  举报