while循环实现链表选择排序
时间: 2023-10-09 22:17:44 浏览: 28
可以这样实现:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def selectionSort(head):
if not head:
return None
dummy = ListNode(0)
dummy.next = head
cur = head
while cur.next:
pre = cur
min_node = cur
while pre.next:
if pre.next.val < min_node.val:
min_node = pre.next
pre = pre.next
if cur != min_node:
temp = cur.val
cur.val = min_node.val
min_node.val = temp
cur = cur.next
return dummy.next
```
希望能帮到你!如果还有其它问题,欢迎随时问我。