Advertisement
imashutosh51

Absolute linked list sorting

Aug 10th, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. /*
  2. Logic:
  3.  if number is positive,then no change
  4.  else number will be in left side of head node and head node will be updated.
  5. */
  6. class Solution{
  7.    
  8. public:
  9.     Node* sortList(Node* head)
  10.     {
  11.          Node *cur=head->next,*prev=head;
  12.          while(cur){
  13.              if(cur->data<0){
  14.                  prev->next=cur->next;
  15.                  Node *temp=cur;
  16.                  temp->next=head;
  17.                  head=temp;
  18.                  
  19.                  cur=prev->next;  //cur=cur->next not here because cur is removed
  20.                  continue;        //and prev will be same because node is removed.
  21.              }
  22.              prev=cur;            //normal shifting of node if no nodes deleted.
  23.              cur=cur->next;
  24.          }
  25.          return head;
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement