从链表的第一个节点开始,找到最小的元素进行交换。依次进行,找到次小的元素。。。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
ListNode* sortInList(ListNode* head) {
// write code here
ListNode* p = head;
ListNode* q = head;
while(p)
{
q = p;
while(q)
{
if(q->val < p->val)
{
swap(q->val, p->val);
}
q = q->next;
}
p = p->next;
}
return head;
}
};
京公网安备 11010502036488号