博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode之Reverse Nodes in k-Group (Hard)
阅读量:2343 次
发布时间:2019-05-10

本文共 2786 字,大约阅读时间需要 9 分钟。

题意:

把一个链表每 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

转载地址:http://zmbvb.baihongyu.com/

你可能感兴趣的文章
费雪耶兹(Fisher–Yates) 也被称作高纳德( Knuth)随机置乱算法
查看>>
C/C++中变量的存储位置
查看>>
C++中四种强制类型转换区别详解
查看>>
linux gdb的详细用法 运行与断点
查看>>
删除vector中重复元素
查看>>
和为s的连续正数序列
查看>>
什么是Redis?什么是nosql?NoSQL数据库的四大分类
查看>>
为什么说Redis是单线程的以及Redis为什么这么快!
查看>>
redis的过期健删除策略以及内存淘汰机制
查看>>
map 如何使用结构体作为自定义键值
查看>>
Mysql几种索引类型的区别及适用情况
查看>>
Redis持久化的两种方式
查看>>
判断一个数组,是否可以分成两个数组之和相等的数组
查看>>
背包问题
查看>>
结构体变量之间的比较和赋值原理
查看>>
C++ const修饰函数、函数参数、函数返回值
查看>>
将单链表的每k个节点之间逆序
查看>>
删除链表中重复的节点——重复节点不保留
查看>>
2018腾讯校招编程题——最重要的城市
查看>>
删除链表中重复的节点——重复节点保留一个
查看>>