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 {
27
27
return head;
28
28
}
29
29
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++;
38
36
}
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;
39
41
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
+ }
41
46
42
- p2-> next = head;
43
- head = p1 ->next ;
44
- p1 ->next = NULL ;
47
+ // break the list
48
+ head = p ->next ;
49
+ p ->next = NULL ;
45
50
46
51
return head;
47
-
52
+
48
53
}
49
54
};
You can’t perform that action at this time.
0 commit comments