File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
src/Algorithms/0147.insertion-sort-list Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a singly-linked list.
3
+ * class ListNode {
4
+ * public $val = 0;
5
+ * public $next = null;
6
+ * function __construct($val) { $this->val = $val; }
7
+ * }
8
+ */
9
+ class Solution {
10
+
11
+ /**
12
+ * @param ListNode $head
13
+ * @return ListNode
14
+ */
15
+ function insertionSortList ($head ) {
16
+ if ($head == null || $head -> next == null ) return $head ;
17
+ $tmp ;
18
+ $k ; $z ;
19
+ for ($start = $head ; $start -> next != null ; $start = $start -> next ) {
20
+ $tmp = $start -> next ;
21
+ $k = $tmp -> val ;
22
+ $end ;
23
+ for ($end = $head ; $end != $start -> next ; $end = $end -> next ) {
24
+ if ($tmp -> val < $end -> val ) {
25
+ $z = $end -> val ;
26
+ $end -> val = $k ;
27
+ $k = $z ;
28
+ }
29
+ }
30
+ $end -> val = $k ;
31
+ }
32
+ return $head ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments