File tree Expand file tree Collapse file tree 1 file changed +18
-13
lines changed Expand file tree Collapse file tree 1 file changed +18
-13
lines changed Original file line number Diff line number Diff line change @@ -27,23 +27,28 @@ class Solution {
2727 return head;
2828 }
2929
30- ListNode *p1, *p2=head;
31- for (int i=0 ; i<k; i++){
32- if (p2->next !=NULL ){
33- p2 = p2->next ;
34- } else {
35- // Shit! the K also rotated!
36- p2 = head;
37- }
30+ // find the length of List
31+ int len=1 ;
32+ ListNode *p=head;
33+ while ( p->next != NULL ){
34+ p = p->next ;
35+ len++;
3836 }
37+ // connect the tail to head
38+ p->next = head;
39+ // find the left place (take care the case - k > len)
40+ k = len - k % len;
3941
40- for (p1=head; p2->next !=NULL ; p1=p1->next , p2=p2->next );
42+ // find the place
43+ for (int i=0 ; i<k; i++){
44+ p = p->next ;
45+ }
4146
42- p2-> next = head;
43- head = p1 ->next ;
44- p1 ->next = NULL ;
47+ // break the list
48+ head = p ->next ;
49+ p ->next = NULL ;
4550
4651 return head;
47-
52+
4853 }
4954};
You can’t perform that action at this time.
0 commit comments