zoukankan      html  css  js  c++  java
  • LeetCode Rotate List

    class Solution {
    public:
        ListNode *rotateRight(ListNode *head, int k) {
            if (head == NULL) return NULL;
            ListNode *p = head, *q = head;
    
            int dst = k;
            while (dst--) {
                q = q->next;
                if (q == NULL) {
                    q = head;
                    k = k % (k - dst);
                    dst = k;
                }
            }
            if (p == q) return head;
            while (q->next != NULL) {
                q = q->next;
                p = p->next;
            }
            ListNode *new_head = p->next;
            p->next = NULL;
            q->next = head;
            return new_head;
        }
    };

    中间进行适当的判断防止k>>list.length,而list.length又很大的情况下,重复遍历链表时间的浪费,这样在k>=list.length的情况下都只需完整遍历一次链表即可

    第二轮:

    Given a list, rotate the list to the right by k places, where k is non-negative.

    For example:
    Given 1->2->3->4->5->NULL and k = 2,
    return 4->5->1->2->3->NULL.

    感觉脑子进水了,链表可以直接移动啊,艹!

     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  // 10:00
    10 class Solution {
    11 public:
    12     ListNode *rotateRight(ListNode *head, int k) {
    13         if (head == NULL) return NULL;
    14         
    15         int cnt = 0;
    16         ListNode* cur = head;
    17         while (cur) {
    18             cnt++;
    19             cur = cur->next;
    20         }
    21         k = k % cnt;
    22         
    23         if (k == 0) return head;
    24         
    25         ListNode* rhead = reverse(head, cnt);
    26         
    27         int sk = k;
    28         cur = rhead;
    29         while (sk--) {
    30             cur = cur->next;
    31         }
    32         
    33         ListNode* rhead_part1 = reverse(rhead, k);
    34         ListNode* rhead_part2 = reverse(cur, cnt - k);
    35         
    36         if (rhead_part1 != NULL) {
    37             rhead->next = rhead_part2;
    38         }
    39         
    40         return rhead_part1 == NULL ? rhead_part2 : rhead_part1;
    41     }
    42     
    43     ListNode* reverse(ListNode* head, int k) {
    44         ListNode* pre = NULL;
    45         ListNode* cur = head;
    46         while (k && cur) {
    47             ListNode* next = cur->next;
    48             cur->next = pre;
    49             pre = cur;
    50             cur = next;
    51             k--;
    52         }
    53         return pre;
    54     }
    55 };

     赶紧再写一发:

     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     ListNode *rotateRight(ListNode *head, int k) {
    12         if (head == NULL) {
    13             return NULL;
    14         }
    15         int cnt = 0;
    16         ListNode* pre = NULL;
    17         ListNode* cur = head;
    18         while (cur) {
    19             cnt++;
    20             pre = cur;
    21             cur = cur->next;
    22         }
    23         k = k % cnt;
    24         if (k == 0) {
    25             return head;
    26         }
    27         
    28         ListNode* last = pre;
    29         pre = NULL;
    30         cur = head;
    31         int rk = cnt - k;
    32         while (rk--) {
    33             pre = cur;
    34             cur = cur->next;
    35         }
    36         last->next= head;
    37         pre->next = NULL;
    38         return cur;
    39     }
    40 };
  • 相关阅读:
    ORACLE获取DML(Insert into)的方法
    联动
    浏览器插件使用
    tomcat 修改用户名和密码
    Oracle单行函数
    CVS团队源代码管理
    jotm的xml
    ORACLE获取DDL(Create Table)的几种常用的方法
    正则表达式详解
    java.lang.NoClassDefFoundError
  • 原文地址:https://www.cnblogs.com/lailailai/p/3671162.html
Copyright © 2011-2022 走看看