Skip to content

Commit e1dd964

Browse files
committed
Add solution 147.
1 parent 40d75e1 commit e1dd964

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)