Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created April 21, 2018 04:23
Show Gist options
  • Save Kishy-nivas/eb19a231eeeeb2c2d4644dc6296e2933 to your computer and use it in GitHub Desktop.
Save Kishy-nivas/eb19a231eeeeb2c2d4644dc6296e2933 to your computer and use it in GitHub Desktop.
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
/* The structure of the node of the Linked List is
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
*/
class GfG
{
Node sortedList(Node head)
{
Node curr = head;
Node prev = null;
Node next = null;
while (curr != null){
if(curr.data <0 && curr != head){ // no nead to change if the head is negative already
next = curr.next; // store the next values of current
curr.next = head; // make current as head
head = curr;
prev.next = next; // connect the remaining elements
curr = prev; // update current element
}
prev = curr;
curr = curr.next;
}
return head;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment