Linked List-----25. Reverse Nodes in k-Group

原题目

虽然是使用LinkedList的题目,但是我还是使用stack来做了,但是代码实在是太乱了,待会儿整理吧,毕竟现在是凌晨五点了,可是我还是没有一点睡意,显然我都不知道自己在修改一些什么东西了。
显然比较棘手的是处理结尾的地方,有四种可能的情况要处理

stack

  public ListNode reverseKGroup(ListNode head, int k) {
        Stack<Integer> stack = new Stack<>();
        ListNode p = head;
        ListNode dummy = null;
        ListNode q = dummy;
        int counter = 0;
        while (p != null) {
            if (counter != k) {
                stack.push(p.val);
                counter++;
                p = p.next;
            } else {
                for (int i = 0; i < k; i++) {
                    int a = stack.pop();
                    ListNode node = new ListNode(a);
                    if (dummy == null) {
                        dummy = node;
                        q = dummy;
                    } else {
                        q.next = node;
                        q = q.next;
                    }
                }
                counter = 0;
            }
        }
        if (counter != 0) {
            if (q != null && counter < k) {
                for (Integer i : stack) {
                    ListNode node = new ListNode(i);
                    q.next = node;
                    q = q.next;
                }
            } else if (q != null && counter == k) {
                while (!stack.isEmpty()) {
                    ListNode node = new ListNode(stack.pop());
                    if (dummy == null) {
                        dummy = node;
                        q = dummy;
                    } else {
                        q.next = node;
                        q = q.next;
                    }
                }
            } else if (q == null && counter < k) {
                dummy = head;
            } else if (q == null && counter == k) {
                while (!stack.isEmpty()) {
                    ListNode node = new ListNode(stack.pop());
                    if (dummy == null) {
                        dummy = node;
                        q = dummy;
                    } else {
                        q.next = node;
                        q = q.next;
                    }
                }
            }
        }
        print(dummy);
        return dummy;

    }

整理


    public ListNode reverseKGroup(ListNode head, int k) {
        Stack<Integer> stack = new Stack<>();
        ListNode p = head;
        ListNode dummy = null;
        ListNode q = dummy;
        int counter = 0;
        //遍历吞栈,每一个小分段就吐出来直到最后又剩余的一小部分,对于这小部分有几种情况
        while (p != null) {
            if (counter != k) {
                stack.push(p.val);
                counter++;
                p = p.next;
            } else {
                while (!stack.isEmpty()) {
                    ListNode node = new ListNode(stack.pop());
                    if (dummy == null) {
                        dummy = node;
                        q = dummy;
                    } else {
                        q.next = node;
                        q = q.next;
                    }
                }
                counter = 0;
            }
        }
        //还有剩余的尾部
        if (counter != 0) {
            //尾部刚好能够一个栈,分两种情况,一种是没有进行过操作,一种是已经经历过操作的,没有经历过操作的话dummy为空
            if (counter == k) {
                while (!stack.isEmpty()) {
                    ListNode node = new ListNode(stack.pop());
                    if (dummy == null) {
                        dummy = node;
                        q = dummy;
                    } else {
                        q.next = node;
                        q = q.next;
                    }
                }
            } else {
                //尾部不够一个栈且已经经历过操作,q不为空
                if (q != null) {
                    for (Integer i : stack) {
                        ListNode node = new ListNode(i);
                        q.next = node;
                        q = q.next;
                    }
                } else {//尾部不够一个栈,但是没有经历过操作,直接返回原链
                    dummy = head;
                }
            }

        }
        print(dummy);
        return dummy;

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值