LeetCode Reverse Nodes in k-Group

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:24
10 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:53
10 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 };

 

posted @ 2014-07-27 23:18  卖程序的小歪  阅读(225)  评论(0编辑  收藏  举报