题解 | #单链表的排序#
单链表的排序
http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
struct ListNode* sortInList(struct ListNode* head ) {
struct ListNode* temp=head;//保存头节点原来指向
int n=0;
while(head!=NULL)
{
head=head->next;
n++;
}
head=temp;//head指针重新指向头节点
int i,j;
for(i=0;i<n-1;i++)
{
head=temp;
for(j=0;j<n-i-1;j++)
{
if(head->val>=head->next->val)
{
int temt;
temt=head->val;
head->val=head->next->val;
head->next->val=temt;
}
head=head->next;
}
}
head=temp;
return head;
// write code here
}