欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言數(shù)據(jù)結(jié)構(gòu)旋轉(zhuǎn)鏈表的實現(xiàn)

 更新時間:2017年08月20日 15:11:56   投稿:lqh  
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)旋轉(zhuǎn)鏈表的實現(xiàn)的相關(guān)資料,這里提供實例幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下

C語言數(shù)據(jù)結(jié)構(gòu)旋轉(zhuǎn)鏈表的實現(xiàn)

實例:

給出鏈表1->2->3->4->5->null和k=2

返回4->5->1->2->3->null

分析

感覺很直觀,直接把分割點找出來就行,記得k可能大于len,要取模

代碼:

/** 
 * Definition for singly-linked list. 
 * struct ListNode { 
 *   int val; 
 *   ListNode *next; 
 *   ListNode(int x) : val(x), next(NULL) {} 
 * }; 
 */ 
class Solution { 
public: 
  /** 
   * @param head: the list 
   * @param k: rotate to the right k places 
   * @return: the list after rotation 
   */ 
  ListNode *rotateRight(ListNode *head, int k) { 
    // write your code here 
    if(head==NULL) 
      return head; 
    int len = 0; 
    ListNode*temp = head; 
    while(temp) 
    { 
      len++; 
      temp = temp->next; 
    } 
    k%=len; 
    if(k==0) 
      return head; 
    k = len-k; 
    temp = head; 
    while(k>1) 
    { 
      temp = temp->next; 
      k--; 
    } 
    ListNode*newStart = temp->next; 
    temp->next = NULL; 
    temp = newStart; 
    while(temp->next) 
      temp = temp->next; 
    temp->next = head; 
    return newStart; 
  } 
}; 

 以上就是C語言數(shù)據(jù)結(jié)構(gòu)旋轉(zhuǎn)鏈表的實現(xiàn),如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于數(shù)據(jù)結(jié)構(gòu)的文章還有很多,希望大家搜索查閱,大家共同進步!

相關(guān)文章

最新評論