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 ) return head; 7 ListNode * root= new ListNode(-1); 8 root->next = head; 9 while( k>0 ) 10 { 11 k--; 12 head=head->next; 13 if( head == NULL ) 14 head = root->next; 15 } 16 ListNode * p1 = root->next; 17 while( head->next!= NULL ) 18 { 19 p1 = p1->next; 20 head = head -> next; 21 } 22 if( p1->next == NULL ) return root -> next; 23 ListNode * tmp = root->next; 24 root->next = p1->next; 25 p1 -> next = NULL; 26 head -> next = tmp; 27 return root->next; 28 } 29 };
posted on 2013-07-04 14:33 jumping_grass 阅读(121) 评论(0) 收藏 举报
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号