博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Reverse Nodes in k-Group
阅读量:4925 次
发布时间:2019-06-11

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

class Solution {public:    ListNode *reverseKGroup(ListNode *head, int k) {        if (k < 1) return head;        ListNode* last = NULL;        ListNode* newhead = NULL;        ListNode* cur = head;        bool fullcut = false;        while (cur != NULL) {            ListNode* remain_head = cut(cur, k, fullcut);            ListNode* rtail = cur;            ListNode* rhead = fullcut ? reverse(cur) : cur;            cur = remain_head;            if (newhead == NULL) {                newhead = rhead;            } else {                last->next = rhead;            }            last = rtail;        }                return newhead;    }        ListNode* cut(ListNode* head, int k, bool &full) {        ListNode* cur = head;        ListNode* pre = NULL;        while (k > 0 && cur != NULL) {            k--;            pre = cur;            cur = cur->next;        }        if (pre != NULL) pre->next = NULL;        full = k == 0;        return cur;    }        ListNode* reverse(ListNode* head) {        ListNode* cur = head;        ListNode* pre = NULL;        while (cur != NULL) {            ListNode* t = cur->next;            cur->next = pre;            pre = cur;            cur = t;        }        return pre;    }};

对于是否完全是k个元素的处理有点脏(几个变量的含义与刚好是k个元素时不一致,不过因为不是k个的情况只会发生一次且是最后一次迭代,因而这些变量变脏了也无妨),不过时间有点长130ms+不知是否可以继续改进

 

第二轮:

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

强势回归!

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9  // 20:2410 class Solution {11 public:12     ListNode *reverseKGroup(ListNode *head, int k) {13         ListNode dummyHead(0);14         dummyHead.next = head;15         16         ListNode* last_tail = &dummyHead;17         18         ListNode* pre = NULL;19         ListNode* cur = dummyHead.next;20         ListNode* rtail = cur;21         22         ListNode* prob = cur;23         24         int tk = k;25         while (prob != NULL && tk != 0) {26             prob = prob->next;27             tk--;28         }29         30         while (!tk) {31             while (cur != prob) {32                 ListNode* tmp = cur->next;33                 cur->next = pre;34                 pre = cur;35                 cur = tmp;36             }37             last_tail->next = pre;38             last_tail = rtail;39             40             last_tail->next = cur;41             rtail = cur;42             pre = NULL;43             44             tk = k;45             46             while (prob != NULL && tk != 0) {47                 prob = prob->next;48                 tk--;49             }50         }51         52         return dummyHead.next;53     }54 };

 找到一个递归版本也挺好的:

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9  // recursive version: 20:5310 class Solution {11 public:12     ListNode *reverseKGroup(ListNode *head, int k) {13         ListNode* cur = head;14         int tk = k;15         while (tk && cur) {16             cur = cur->next;17             tk--;18         }19         20         if (!tk) {21             ListNode* pre = reverseKGroup(cur, k);22             cur = head;23             tk = k;24             while (tk--) {25                 ListNode* tmp = cur->next;26                 cur->next = pre;27                 pre = cur;28                 cur = tmp;29             }30             head = pre;31         }32         return head;33     }34 };

 

转载于:https://www.cnblogs.com/lailailai/p/3872161.html

你可能感兴趣的文章
RESTful-rest_framework应用第一篇
查看>>
Console命令详解,让调试js代码变得更简单
查看>>
hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)
查看>>
Excel 导出
查看>>
拉登是我罩的队_第三周_需求改进&原型设计
查看>>
数据库got error 28 from storage engine问题
查看>>
RMQ 总结
查看>>
手撸ORM
查看>>
POJ---2406 Power Strings[求最长重复字串]
查看>>
005-(已测试成功的方案)kickstart模式实现批量安装centos7.x系统
查看>>
linux搭建haproxy
查看>>
Oracle update 日期
查看>>
【t088】倒水
查看>>
【t016】邮递员
查看>>
boost安装
查看>>
Vue与React的异同
查看>>
360:跳高游戏
查看>>
CSS3 Background-size
查看>>
Python Ethical Hacking - MAC Address & How to Change(3)
查看>>
生成验证码
查看>>