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;
}



作业部落

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值