Skip to content

Commit

Permalink
fix issue#32, and make the code more clean
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Nov 20, 2014
1 parent 3fbb652 commit 356254b
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/rotateList/rotateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@ class Solution {
return head;
}

ListNode *p1, *p2=head;
for(int i=0; i<k; i++){
if (p2->next!=NULL){
p2 = p2->next;
} else {
//Shit! the K also rotated!
p2 = head;
}
//find the length of List
int len=1;
ListNode *p=head;
while( p->next != NULL ){
p = p->next;
len++;
}
//connect the tail to head
p->next = head;
//find the left place (take care the case - k > len)
k = len - k % len;

for(p1=head; p2->next!=NULL; p1=p1->next, p2=p2->next);
//find the place
for(int i=0; i<k; i++){
p = p->next;
}

p2->next = head;
head = p1->next;
p1->next = NULL;
//break the list
head = p->next;
p->next = NULL;

return head;

}
};

0 comments on commit 356254b

Please sign in to comment.