K个一组反转链表

学到的写法

 public ListNode reverseKGroup_v2(ListNode head, int k){
        ListNode pHead = new ListNode(-1, head);
        ListNode pre = pHead;
        while(true){
            ListNode last =  pre.next;
            for(int i=0;i<k;i++){
                if (last== null){
                    return pHead.next;
                }
                last = last.next;
            }
            ListNode cur = pre.next;
            for (int j=0;j<k-1;j++){
                ListNode next = cur.next;
                cur.next = next.next;
                next.next = pre.next;
                pre.next = next;
            }
            pre = cur;
        }
    }

自己一遍过的愚蠢代码,虽蠢但过

private  Map<ListNode, Integer> listNodeMap  = new HashMap<>();
    private  Map<Integer,ListNode>listNodeMapR = new HashMap<>();
    /**
     * 存放每k段头尾的映射,为了再次连接
     */
    private  Map<ListNode, ListNode> listNodeMapTail = new HashMap<>();
    private  List<ListNode> subListNodes = new ArrayList<>();
    public  ListNode reverseKGroup(ListNode head, int k) {
        ListNode pHead = head;
        int count = 0;
        while(pHead != null){
            count++;
            listNodeMap.put(pHead, count);
            listNodeMapR.put(count,pHead);
            pHead =  pHead.next;
        }

        ListNode result = new ListNode(-1);
        ListNode temp = result;
        cutKListNode(head,k);
        for (ListNode node:subListNodes){
            int addKIndex = listNodeMap.get(node)+k-1;
            if (addKIndex<=count){
                ListNode subListNode = reverseListNode(node, k);
                temp.next = subListNode;
                temp = listNodeMapTail.get(subListNode);
            }else {
                temp.next = node;
            }
        }
        return result.next;
    }


    /**
     * 反转链表
     * @param listNode
     * @param k
     * @return
     */
    private  ListNode reverseListNode(ListNode listNode,Integer k){
        ListNode newNode = new ListNode(-1);
        int currentIndex = listNodeMap.get(listNode);
        ListNode subTail = listNode;
        while(listNode != null && listNodeMap.get(listNode)-currentIndex<k){
            listNodeMapTail.put(listNode, subTail);
            ListNode nextNode = listNode.next;
            listNode.next = newNode.next;
            newNode.next = listNode;
            listNode = nextNode;
        }
        return newNode.next;
    }

    /**
     * 切链表为每k一段
     * @param head
     * @param k
     */
    private void cutKListNode(ListNode head, int k){
        while(head != null){
            subListNodes.add(head);
            int index = listNodeMap.get(head);
            head = listNodeMapR.get(index+k);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值