How to sort ListNode (linked) by its value?How do I check if an array includes an object in JavaScript?How to detect a loop in a linked list?Fastest sort of fixed length 6 int arraycheck if all elements in a list are identicalInterview Question: Merge two sorted singly linked lists without creating new nodesHow to pair socks from a pile efficiently?Is there a way to measure how sorted a list is?removing even indexes' values in a linked listWhat's wrong with my code to delete duplicate-value nodes from a sorted linked list?Linked list merge sort using java

Are wands in any sort of book going to be too much like Harry Potter?

GLM: Modelling proportional data - account for variation in total sample size

Mindfulness of Watching Youtube

How long can fsck take on a 30 TB volume?

Creating Stored Procedure in local db that references tables in linked server

How can I test a shell script in a "safe environment" to avoid harm to my computer?

Why are thrust reversers not used down to taxi speeds?

Employee is self-centered and affects the team negatively

Why doesn't a particle exert force on itself?

Why did Ham the Chimp push levers?

Capturing the entire webpage with WebExecute's CaptureImage

Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1

Expl3 and recent xparse on overleaf: No expl3 loader detected

What are my options legally if NYC company is not paying salary?

My Sixteen Friendly Students

My perfect evil overlord plan... or is it?

Is it a good idea to copy a trader when investing?

And now you see it II (the B side)

History: Per Leviticus 19:27 would the apostles have had corner locks ala Hassidim today?

Are there vaccine ingredients which may not be disclosed ("hidden", "trade secret", or similar)?

What's an appropriate age to involve kids in life changing decisions?

How could a civilization detect tachyons?

Why is it wrong to *implement* myself a known, published, widely believed to be secure crypto algorithm?

Visual Studio Code download existing code



How to sort ListNode (linked) by its value?


How do I check if an array includes an object in JavaScript?How to detect a loop in a linked list?Fastest sort of fixed length 6 int arraycheck if all elements in a list are identicalInterview Question: Merge two sorted singly linked lists without creating new nodesHow to pair socks from a pile efficiently?Is there a way to measure how sorted a list is?removing even indexes' values in a linked listWhat's wrong with my code to delete duplicate-value nodes from a sorted linked list?Linked list merge sort using java






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















How do you sort nodelist by value without using any library.



Example:
* Input: 3->1->5->4->2
* Output: 1->2->3->4->5



ListNode.java



import java.util.List;

public class ListNode
public int val;
public ListNode next;

public ListNode(int x)
val = x;


public ListNode(int val, ListNode next)
this.val = val;
this.next = next;




SortLinkList.java



public class SortLinkList 

public static ListNode sortLinkList(ListNode list)
//TODO:
return list;


```









share|improve this question






























    0















    How do you sort nodelist by value without using any library.



    Example:
    * Input: 3->1->5->4->2
    * Output: 1->2->3->4->5



    ListNode.java



    import java.util.List;

    public class ListNode
    public int val;
    public ListNode next;

    public ListNode(int x)
    val = x;


    public ListNode(int val, ListNode next)
    this.val = val;
    this.next = next;




    SortLinkList.java



    public class SortLinkList 

    public static ListNode sortLinkList(ListNode list)
    //TODO:
    return list;


    ```









    share|improve this question


























      0












      0








      0








      How do you sort nodelist by value without using any library.



      Example:
      * Input: 3->1->5->4->2
      * Output: 1->2->3->4->5



      ListNode.java



      import java.util.List;

      public class ListNode
      public int val;
      public ListNode next;

      public ListNode(int x)
      val = x;


      public ListNode(int val, ListNode next)
      this.val = val;
      this.next = next;




      SortLinkList.java



      public class SortLinkList 

      public static ListNode sortLinkList(ListNode list)
      //TODO:
      return list;


      ```









      share|improve this question
















      How do you sort nodelist by value without using any library.



      Example:
      * Input: 3->1->5->4->2
      * Output: 1->2->3->4->5



      ListNode.java



      import java.util.List;

      public class ListNode
      public int val;
      public ListNode next;

      public ListNode(int x)
      val = x;


      public ListNode(int val, ListNode next)
      this.val = val;
      this.next = next;




      SortLinkList.java



      public class SortLinkList 

      public static ListNode sortLinkList(ListNode list)
      //TODO:
      return list;


      ```






      algorithm linked-list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 23 at 10:43









      Mark Setchell

      94.7k786197




      94.7k786197










      asked Mar 23 at 7:31









      The Java GuyThe Java Guy

      92478




      92478






















          2 Answers
          2






          active

          oldest

          votes


















          1














          If you want in-place sort you can just implement bubble-sort:



          Pseudo-code:



          bool notDone = true
          while(notDone)

          notDone = false;
          cur = head;

          while(cur.nxt != null)

          prev = cur;
          cur = cur.nxt;

          if(cur.val > cur.nxt.val)

          prev.nxt = cur.nxt;
          temp = cur.nxt.nxt;
          cur.nxt.nxt = cur;
          cur.nxt = temp;
          notDone = true;








          share|improve this answer























          • The concept is right however it doesn't sort the list.

            – The Java Guy
            Mar 24 at 9:44


















          0














          1.Make an array of the class which only store each node and for each node, next is pointed to null.Length of the array is no of nodes in the list.



          2.Sort the array
          3. Link the nodes and return head



          PS: why are you using linked list when there is a sorting operation involved , instead of using arrays alone?






          share|improve this answer























          • This is algo problem. want to solve without libraries.

            – The Java Guy
            Mar 24 at 12:49











          • The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

            – Argha Chakraborty
            Mar 24 at 13:25












          • Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

            – The Java Guy
            Mar 25 at 20:40












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311631%2fhow-to-sort-listnode-linked-by-its-value%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If you want in-place sort you can just implement bubble-sort:



          Pseudo-code:



          bool notDone = true
          while(notDone)

          notDone = false;
          cur = head;

          while(cur.nxt != null)

          prev = cur;
          cur = cur.nxt;

          if(cur.val > cur.nxt.val)

          prev.nxt = cur.nxt;
          temp = cur.nxt.nxt;
          cur.nxt.nxt = cur;
          cur.nxt = temp;
          notDone = true;








          share|improve this answer























          • The concept is right however it doesn't sort the list.

            – The Java Guy
            Mar 24 at 9:44















          1














          If you want in-place sort you can just implement bubble-sort:



          Pseudo-code:



          bool notDone = true
          while(notDone)

          notDone = false;
          cur = head;

          while(cur.nxt != null)

          prev = cur;
          cur = cur.nxt;

          if(cur.val > cur.nxt.val)

          prev.nxt = cur.nxt;
          temp = cur.nxt.nxt;
          cur.nxt.nxt = cur;
          cur.nxt = temp;
          notDone = true;








          share|improve this answer























          • The concept is right however it doesn't sort the list.

            – The Java Guy
            Mar 24 at 9:44













          1












          1








          1







          If you want in-place sort you can just implement bubble-sort:



          Pseudo-code:



          bool notDone = true
          while(notDone)

          notDone = false;
          cur = head;

          while(cur.nxt != null)

          prev = cur;
          cur = cur.nxt;

          if(cur.val > cur.nxt.val)

          prev.nxt = cur.nxt;
          temp = cur.nxt.nxt;
          cur.nxt.nxt = cur;
          cur.nxt = temp;
          notDone = true;








          share|improve this answer













          If you want in-place sort you can just implement bubble-sort:



          Pseudo-code:



          bool notDone = true
          while(notDone)

          notDone = false;
          cur = head;

          while(cur.nxt != null)

          prev = cur;
          cur = cur.nxt;

          if(cur.val > cur.nxt.val)

          prev.nxt = cur.nxt;
          temp = cur.nxt.nxt;
          cur.nxt.nxt = cur;
          cur.nxt = temp;
          notDone = true;









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 7:49









          PhotonPhoton

          827814




          827814












          • The concept is right however it doesn't sort the list.

            – The Java Guy
            Mar 24 at 9:44

















          • The concept is right however it doesn't sort the list.

            – The Java Guy
            Mar 24 at 9:44
















          The concept is right however it doesn't sort the list.

          – The Java Guy
          Mar 24 at 9:44





          The concept is right however it doesn't sort the list.

          – The Java Guy
          Mar 24 at 9:44













          0














          1.Make an array of the class which only store each node and for each node, next is pointed to null.Length of the array is no of nodes in the list.



          2.Sort the array
          3. Link the nodes and return head



          PS: why are you using linked list when there is a sorting operation involved , instead of using arrays alone?






          share|improve this answer























          • This is algo problem. want to solve without libraries.

            – The Java Guy
            Mar 24 at 12:49











          • The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

            – Argha Chakraborty
            Mar 24 at 13:25












          • Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

            – The Java Guy
            Mar 25 at 20:40
















          0














          1.Make an array of the class which only store each node and for each node, next is pointed to null.Length of the array is no of nodes in the list.



          2.Sort the array
          3. Link the nodes and return head



          PS: why are you using linked list when there is a sorting operation involved , instead of using arrays alone?






          share|improve this answer























          • This is algo problem. want to solve without libraries.

            – The Java Guy
            Mar 24 at 12:49











          • The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

            – Argha Chakraborty
            Mar 24 at 13:25












          • Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

            – The Java Guy
            Mar 25 at 20:40














          0












          0








          0







          1.Make an array of the class which only store each node and for each node, next is pointed to null.Length of the array is no of nodes in the list.



          2.Sort the array
          3. Link the nodes and return head



          PS: why are you using linked list when there is a sorting operation involved , instead of using arrays alone?






          share|improve this answer













          1.Make an array of the class which only store each node and for each node, next is pointed to null.Length of the array is no of nodes in the list.



          2.Sort the array
          3. Link the nodes and return head



          PS: why are you using linked list when there is a sorting operation involved , instead of using arrays alone?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 7:50









          Argha ChakrabortyArgha Chakraborty

          259




          259












          • This is algo problem. want to solve without libraries.

            – The Java Guy
            Mar 24 at 12:49











          • The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

            – Argha Chakraborty
            Mar 24 at 13:25












          • Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

            – The Java Guy
            Mar 25 at 20:40


















          • This is algo problem. want to solve without libraries.

            – The Java Guy
            Mar 24 at 12:49











          • The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

            – Argha Chakraborty
            Mar 24 at 13:25












          • Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

            – The Java Guy
            Mar 25 at 20:40

















          This is algo problem. want to solve without libraries.

          – The Java Guy
          Mar 24 at 12:49





          This is algo problem. want to solve without libraries.

          – The Java Guy
          Mar 24 at 12:49













          The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

          – Argha Chakraborty
          Mar 24 at 13:25






          The solution that is given here does not require any library. I'm assuming you know how to sort (using well known sorting algorithm)an array of objects based on an attribute? Each object being the node of your list.

          – Argha Chakraborty
          Mar 24 at 13:25














          Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

          – The Java Guy
          Mar 25 at 20:40






          Your assumption is right. This will require extra O(n) space + O(n^2) worst case runtime depending on which sorting you use. This is not something that I am using in my production code. This aas one of the algorithm question.

          – The Java Guy
          Mar 25 at 20:40


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311631%2fhow-to-sort-listnode-linked-by-its-value%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript