zoukankan      html  css  js  c++  java
  • leetcode61. Rotate List

    问题比较简单,方法就是k%sum,然后移动这么多次,把这段链表插在首部就搞定了。有几个测试用例比较恶心,k==0 head==NULL和k%sum==0的情况,假如k%sum为0的话根本不用任何操作直接返回head就好。

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    *     int val;
    *     ListNode *next;
    *     ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
        ListNode* rotateRight(ListNode* head, int k) {
            if (head == NULL)
                return NULL;
            if (k == 0)
                return head;
            int sum = 0;
            ListNode* st;
            st = head;
            ListNode* dummyNode;
            dummyNode = new ListNode(0);
            dummyNode->next = head;
            while (st != NULL)
            {
                st = st->next;
                sum++;
            }
            if (sum == 1)
                return head;
            if (k > sum)
            {
                k = k%sum;
            }
            int n;
            n = sum - k;
            st = dummyNode;
            while (n--)
            {
                st = st->next;
            }
            if (k==0)
                return head;
            ListNode* p;
            p= st->next;
            st->next = NULL;
            ListNode* q;
            q = p;
            while (q!=NULL&&q->next != NULL)
            {
                q = q->next;
            }
            q->next = dummyNode->next;
            dummyNode->next = p;
            return dummyNode->next;
        }
    };
  • 相关阅读:
    Spherical Harmonic Lighting
    EV/ISO/Stop
    opengles dither
    rgb颜色空间到xyz空间的映射
    LUT color grading
    tonemapping
    ue4 sequencer
    pbr -physical based rendering
    BRDF
    /sockjs-node/info 报错问题
  • 原文地址:https://www.cnblogs.com/legendcong/p/9716548.html
Copyright © 2011-2022 走看看