Leetcode之Reverse Nodes in k-Group (Hard)

题意:

把一个链表每 k 个分为一组,每组内进行翻转。
只能用常数级的空间。

看到这道题首先想到要分组考虑,通过记住连续三个node来求得结果,写的很复杂。。

public class Solution {
 public ListNode reverseKGroup(ListNode head, int k) {
        if(head == null || k == 1)
            return head;
        ListNode result = head;
        ListNode nextNode = head.next;
        ListNode nextNextNode = nextNode == null ? null : nextNode.next;
        
        int count = 0;
        boolean isFirstTime = true;
        ListNode tail = head;
        int i  = 0;
        for(i = 0; i < k - 1 && tail != null; i++){
            tail = tail.next;
        }
        if(i != k - 1)
            return head;
        
        while(tail != null){
        	i = 0;
            ListNode copyHead = head;
            for(count = 0; count < k - 1; count++){
                nextNode.next = head;
                head = nextNode;
                nextNode = nextNextNode;
                nextNextNode = nextNode == null ? null : nextNode.next;
            }
            if(isFirstTime){
                result = head;
                isFirstTime = false;
            }
       
            head = nextNode;
            tail = head;
            for(; i < k - 1 && tail != null; i++){
                tail = tail.next;
            }//end for
            if(tail == null){
            	copyHead.next = head;
            	break;
            }
            copyHead.next = tail;
            nextNode = head.next;
            nextNextNode = nextNode == null ? null : nextNode.next;
        }//end while
        return result;
    }
}

网上搜了别的解法,发现使用递归是如此简单。。先找到下一组的节点,把本组反转后再递归处理后面的节点

代码如下:

public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode cur = head;
        int cnt = 0;
        // get next group
        while (cur != null && cnt != k) {
            cur = cur.next;
            cnt++;
        }
        if (cnt == k) {
            cur = reverseKGroup(cur, k);
            // reverse
            while (0 <= --cnt) {
                ListNode tmp = head.next;
                head.next = cur;
                cur = head;
                head = tmp;
            }
            head = cur;
        }
        return head;
    }
}

class Solution(object):
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head == None:
            return head
        
        cur = head
        count = 0
        
        for i in range(k):
            if cur != None:
                cur = cur.next
                count += 1
            else:
                break
        if count == k:
            cur = self.reverseKGroup(cur, k)
            
            while count >= 1:
               tmp = head.next;
               head.next = cur
               cur = head
               head = tmp
               count -= 1
            head = cur
        return head
        
        



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值