Absolute List Sorting | Practice | GeeksforGeeks

Given a linked list L of N nodes, sorted in ascending order based on the absolute values of its data,i.e. negative values are considered as positive ones. Sort the linked list according to the actual values, consider negative numbers as negative and positive number as positive.

Example 1:  
Input: 
List: 1, -2, -3, 4, -5
Output: 
List: -5, -3, -2, 1, 4
 
Example 2:  
Input: 
List: 5, -10
Output: 
List: -10, 5
 
Your Task:
You don't need to read or print anyhting. Your Task is to comple the function sortList() which takes the head of the Linked List as input parameter and sort the list in ascending order. Don't create a new Linked List instead rearrange the given List.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)

Constraints
1 ≤  N  ≤ 104
-105 ≤ L[i] ≤ 105
0
tthakare733 days ago
Don't Follow This
 
//java solution -> TC -> 1.29
class GfG{
	Node sortList(Node head) {
		// Your code here
		ArrayList<Integer> adj = new ArrayList<>();
		while(head != null){
		    adj.add(head.data );
		    head = head.next;
		}
		Collections.sort(adj);
		Node returnNull = null;
	
		for(int i = 0; i < adj.size(); i++){
		    if(i == adj.size() - 1){
		        System.out.print(adj.get(i));
		    } else {
		        System.out.print(adj.get(i)+" ");   
		    }
		}
		return returnNull;
	}
}
+1
shahabuddinbravo405 days ago
class Solution{public:   void sortList(Node** head)   {       // Node *ptr=*head;  // T(n)=O(N*logN) : s.c(n)=O(n);              // priority_queue<int,vector<int>,greater<int>> minH;              // while(ptr){       //     minH.push(ptr->data);       //     ptr=ptr->next;       // }              // ptr=*head;              // while(ptr){       //     ptr->data=minH.top();       //     minH.pop();       //     ptr=ptr->next;       // }                     // vector<int>positive;    // T(n)=O(n) : S.C = O(n);       // vector<int>negative;              // Node *ptr=*head;       // while(ptr)       // {       //     if(ptr->data<0){       //         negative.push_back(ptr->data);       //     }       //     else{       //         positive.push_back(ptr->data);       //     }       //     ptr=ptr->next;       // }              // ptr=*head;              // while(ptr){       //     for(int i=0;i<negative.size();i++){       //         ptr->data=negative[negative.size()-1-i];       //         ptr=ptr->next;       //     }                  //     for(int i=0;i<positive.size();i++){       //         ptr->data=positive[i];       //         ptr=ptr->next;       //     }       // }          }}; 
0
rishu1 week ago
class GfG
{
Node sortList(Node head) {
 // Your code here 
 Node prev=head;
 Node curr=head.next;
 while(curr!=null){
     if(curr.data<0){
         Node buff = curr.next;
         curr.next = head;
         prev.next = buff;
         head=curr;
         curr=prev.next;
         continue;
     }
     prev=curr;
     curr=curr.next;
 }
 return head;
}
}
0
vt28522412 months ago
Simple Solution : JAVA
Node sortList(Node head) { Node curr=head; while(curr.next!=null){     if(curr.next.data<0){         Node temp=curr.next;         curr.next=curr.next.next;                  temp.next=head;         head=temp;     }     else{         curr=curr.next;     } }  return head;}
0
owaischem2 months ago
def sortList(head):   n=head   list1=[]      while n!=None:       list1.append(n.data)       n=n.next          list1.sort()      n=head   head=n      count=0   while n!=None:       n.data=list1[count]       n=n.next       count+=1          return  head
0
sharma_nitin_262 months ago
bubble sort

class Solution
{
public:
    void sortList(Node** head){
        Node *temp=*head;
        Node *index=NULL;
        int tep=0;

         while(temp!=NULL)
         {
             index=temp->next;
             while(index!=NULL){
                 if(temp->data>index->data){ 
                      tep=temp->data;
                      temp->data=index->data;
                      index->data=tep;
                 }
                  index=index->next;
             }
             temp=temp->next;
         }
    }
};
0
anirudhkrishna222 months ago
void sortList(Node** head)   {       Node* prev=*head;       Node* cur=prev->next;       while(cur)       {           if(cur->data<prev->data)           {               prev->next=cur->next;               cur->next=*head;               *head=cur;               cur=prev->next;           }           else           {               cur=cur->next;               prev=prev->next;           }       }   }
0
ashmeetsingh78012 months ago
void sortList(Node** head)   {       Node* prev = (*head);       Node* curr = (*head)->next;               while (curr != NULL)       {           if (curr->data < prev->data)           {               prev->next = curr->next;               curr->next = (*head);               (*head) = curr;               curr = prev;           }           else prev = curr;           curr = curr->next;       }   }
0
indiakamanthan3 months ago

class GfG
{
Node sortList(Node head) {
    Node h=head.next;
    Node prev=head;
    Node hh=head;
   
   while(h!=null)
{
    if(h.data<0)
    {
        prev.next=h.next;
        h.next=hh;
        hh=h;
        h=prev.next;
        
    }
    else
    {
        prev=h;
        h=h.next;
    }
}
    prev=null;
return hh;

   
}

}
+1
ap123413 months ago
Solution in Java
class GfG{Node sortList(Node head) {  Node prev = head;  Node curr = head.next;    while(curr != null){      if(curr.data < prev.data){         prev.next = curr.next;         curr.next = head;         head = curr;         curr = prev;      }else{          prev = curr;      }          curr = curr.next;  }  return head;   }} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.