Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- if number is positive,then no change
- else number will be in left side of head node and head node will be updated.
- */
- class Solution{
- public:
- Node* sortList(Node* head)
- {
- Node *cur=head->next,*prev=head;
- while(cur){
- if(cur->data<0){
- prev->next=cur->next;
- Node *temp=cur;
- temp->next=head;
- head=temp;
- cur=prev->next; //cur=cur->next not here because cur is removed
- continue; //and prev will be same because node is removed.
- }
- prev=cur; //normal shifting of node if no nodes deleted.
- cur=cur->next;
- }
- return head;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement