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

来源:互联网 发布:淘宝评价晒图怎么删除 编辑:程序博客网 时间:2024/05/18 03:19

原题目

虽然是使用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;    }
原创粉丝点击