-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion Sort List
More file actions
30 lines (29 loc) · 826 Bytes
/
Insertion Sort List
File metadata and controls
30 lines (29 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
ListNode curr = head;
while (curr != null) {
ListNode temp = head;
while (temp != curr) {
if (temp.val > curr.val) {
int y = temp.val;
temp.val = curr.val;
curr.val = y;
}
temp = temp.next;
// System.out.println(curr.val + temp.val);
}
curr = curr.next;
}
return head;
}
}