• Leetcode | Reverse Nodes in k-Group


    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

    用了两个指针p1和p2,使得p1和p2相差k-1个位置。每次p1和p2区间reverse一下,然后再把同时更新p1和p2.

     1 class Solution {
     2 public:
     3 
     4     void reverse(ListNode *start, ListNode *end) {
     5         ListNode *pre = NULL, *tmp;
     6         while (start != end) {
     7             tmp = start->next;
     8             if (pre) start->next = pre;
     9             pre = start;
    10             start = tmp;
    11         }
    12         end->next = pre;
    13     }
    14     ListNode *reverseKGroup(ListNode *head, int k) {
    15             if (k <= 1) return head;
    16             ListNode *p1 = head, *p2 = head, *pre = NULL, *tmp;
    17             for (int i = 0; i < k - 1; ++i) {
    18                 if (p1 == NULL) return head;
    19                 p1 = p1->next;
    20             }
    21             if (p1 != NULL) head = p1;
    22 
    23             while (p1 != NULL) {
    24                 tmp = p1->next;
    25                 if (pre) pre->next = p1;
    26                 reverse(p2, p1);
    27                 pre = p2; // the tail of previous k-list
    28                 p2 = p1 = tmp; // update p2
    29                 for (int i = 0; i < k - 1; ++i) {
    30                     if (p1 == NULL) {
    31                         break; 
    32                     }
    33                     p1 = p1->next;
    34                 }
    35                 if (p1 == NULL) pre->next = tmp; // the left part is less than k
    36             }
    37 
    38             return head;
    39         }
    40 };

    一开始想一遍走完,边走边reverse,但是太复杂了,所以还是这样吧。。。

    另一种思路,每次都找到第k个元素,迭代就行,不用这种两个指针的方法。写起来也比较简洁一点。

     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 class Solution {
    10 public:
    11     void reverse(ListNode *&head, ListNode *&tail) {
    12         ListNode *prev = NULL, *p = head, *tmp;
    13         while (p) {
    14             tmp = p->next;
    15             p->next = prev;
    16             prev = p;
    17             p = tmp;
    18         }
    19         tail = head;
    20         head = prev;
    21     }
    22     
    23     ListNode *reverseKGroup(ListNode *head, int k) {
    24         if (head == NULL) return head;
    25         
    26         ListNode h(0), *cur = head, *next, *prev = &h, *p = head;
    27         h.next = head;
    28         
    29         while (cur) {
    30             p = cur;
    31             int i = 1;
    32             for (; i < k && p; p = p->next, i++);
    33             if (!p) break;
    34             next = p->next;
    35             p->next = NULL;
    36             reverse(cur, p);
    37             prev->next = cur;
    38             p->next = next;
    39             prev = p;
    40             cur = next;
    41         }
    42         
    43         return h.next;
    44     }
    45 
    46 };
  • 相关阅读:
    Kotlin使用常见问题汇总
    浅谈Kotlin(五): 静态变量&静态方法
    浅谈Kotlin(八):空安全、空类型
    浅谈Kotlin(七):lateinit、by lazy 使用
    浅谈Kotlin(六):data class的使用
    实例:([Flappy Bird Qlearning]
    强化学习之MountainCarContinuous(注册自己的gym环境)
    seq2seq之双向解码
    AwesomeVisualCaptioning
    VUE hash路由和history路由的区别
  • 原文地址:https://www.cnblogs.com/linyx/p/3733980.html
Copyright © 2020-2023  润新知