zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    线段树 hdu3255 Farming
    3.CCFadeOutTRTiles,部落格效果,跳动的方块特效,3D瓷砖晃动特效,破碎的3D瓷砖特效,瓷砖洗牌特效,分多行消失特效,分多列消失特效
    分析:新建短信,当我们接受人RecipientsEditor中输入+86的时候,系统会自己主动在+86后增加空格
    【POJ3377】Ferry Lanes 最短路
    Please ensure that adb is correctly located at &#39;D:Androidandroid-sdkplatform-toolsadb.exe&#39; and
    Objective-C
    分布式高维空间近邻搜索项目开发
    我的改进版2048(2)
    github关联域名,创建个人站点教程终结篇
    数据结构(6)二叉树
  • 原文地址:https://www.cnblogs.com/linyx/p/3733980.html
Copyright © 2011-2022 走看看