每日演算法之四十三:Rotate List (列表旋轉k個元素)

來源:互聯網
上載者:User

標籤:鏈表旋轉

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.

這裡的k可能是比鏈表長度要大的數字,因此實際旋轉的位置就是k%len(list)。如果這個計算結果等於零或者等於len(list),列表是不用旋轉的。

接下來如果是案例給出的正常情況,那麼有三步就能完成旋轉。

(1)第一個輔助指標從頭開始後移k個位置。

(2)這時第二個輔助指標指向頭指標,然後兩個指標同時向後移動,知道第一個輔助指標指向尾節點。

(3)聲明第三個輔助指標指向第二個輔助指標的後繼結點作為將要返回的新頭指標。把第二個指標的後繼設為空白指標,同時將第一個指標的後繼指向原先的有指標。這兒樣就能完成指標的旋轉。

註:最後給出的另外另種方式更簡潔高效,推薦先看一下。

/** * 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 || k <= 0)return head;ListNode *temp = head;int node_count = 0;while(temp != NULL){    node_count++;    temp = temp->next;}if(k > node_count)   k = k%node_count;if(k == node_count || k == 0)   return head;ListNode *first = head;while(/*first != NULL && first->next != NULL &&*/ k > 0){first = first->next;k--;}//if(k > 0)//return head;ListNode *second = head;while(first->next != NULL){first = first->next;second = second->next;}ListNode *newhead = second->next;first->next = head;second->next = NULL;return newhead;}};

這裡有一個地方需要注意,在第一次完成的時候沒有代碼中的取餘部分,因為沒有考慮到迴圈移位的位元比鏈表長度長的情況,因此,在加上取餘部分後,原代碼中(即現在注釋掉的部分)是不再需要的。因為這個時候的k一定比鏈表長度要小。因此這些判斷是不在需要的。

下面是一些網上的答案,貼出來以供下次參考。

 ListNode *rotateRight(ListNode *head, int k) {          // Start typing your C/C++ solution below          // DO NOT write int main() function          if (head == NULL || head->next == NULL || k == 0) {              return head;          }                    int length = 0;          ListNode *ptr = head, *tail = head;          while (ptr != NULL) {              length++;              tail = ptr;              ptr = ptr->next;           }          k %= length;                    ptr = head;          for (int i = 0; i < length - k - 1; i++) {              ptr = ptr-> next;          }                    tail->next = head;          head = ptr->next;          ptr->next = NULL;                    return head;      }  
另一種更簡潔的方式。

ListNode *rotateRight(ListNode *head, int k)    {        if(head==NULL)return NULL;        ListNode *p=head;        int n=0;        while(p->next)        {            p=p->next;            n++;        }        n++;        k=k%n;        p->next=head;        ListNode *q=head;        for(int i=0;i<n-k-1;i++)        q=q->next;        head=q->next;        q->next=NULL;            return head;    }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.