mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
524 B
34 lines
524 B
package solutions |
|
|
|
type Nodelist struct { |
|
Data int |
|
Next *Nodelist |
|
} |
|
|
|
func SortList(l *Nodelist, cmp func(a, b int) bool) *Nodelist { |
|
|
|
head := l |
|
if head == nil { |
|
return nil |
|
} |
|
head.Next = SortList(head.Next, cmp) |
|
|
|
if head.Next != nil && cmp(head.Data, head.Next.Data) { |
|
head = moveValue(head, cmp) |
|
} |
|
return head |
|
} |
|
|
|
func moveValue(l *Nodelist, cmp func(a, b int) bool) *Nodelist { |
|
p := l |
|
n := l.Next |
|
ret := n |
|
|
|
for n != nil && cmp(l.Data, n.Data) { |
|
p = n |
|
n = n.Next |
|
} |
|
p.Next = l |
|
l.Next = n |
|
return ret |
|
}
|
|
|