rotate list
1 class Solution { 2 public: 3 ListNode *rotateRight(ListNode *head, int k) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if( head == NULL || k == 0 ) return head; 7 ListNode * root = new ListNode (0); 8 root -> next = head; 9 ListNode * p = head; 10 11 for(int i=1;i<=k;i++) 12 { 13 p = p -> next; 14 if( p == NULL ) p = head; 15 } 16 if( p == head ) return head; 17 18 ListNode * t = head; 19 while( p->next != NULL) 20 { 21 p = p->next; 22 t = t->next; 23 } 24 root->next = t->next; 25 t -> next = NULL; 26 p -> next = head; 27 28 return root->next; 29 } 30 };
posted on 2013-09-03 21:18 jumping_grass 阅读(139) 评论(0) 收藏 举报
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号