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;
}
}```