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;
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
add a comment |
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
add a comment |
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
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
algorithm linked-list
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
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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;
The concept is right however it doesn't sort the list.
– The Java Guy
Mar 24 at 9:44
add a comment |
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?
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
The concept is right however it doesn't sort the list.
– The Java Guy
Mar 24 at 9:44
add a comment |
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;
The concept is right however it doesn't sort the list.
– The Java Guy
Mar 24 at 9:44
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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?
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
add a comment |
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?
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
add a comment |
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?
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?
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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