Insertion Sort List

简介: Sort a linked list using insertion sort. C++代码如下: #include #include using namespace std; //Definition for singly-linked list.

Sort a linked list using insertion sort.

C++代码如下:

#include<iostream>
#include<new>
using namespace std;


//Definition for singly-linked list.
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    ListNode *insertionSortList(ListNode *&head)
    {
        if(head==NULL)
            return NULL;
        ListNode *pre=head;
        ListNode *p=head;
        ListNode *q=head->next;
        ListNode *tmp=NULL;
        head->next=NULL;
        while(q)
        {
            tmp=q->next;
            q->next=NULL;
            while(p)
            {
                if(q->val>p->val)
                {
                    pre=p;
                    p=p->next;
                }
                else
                {
                    if(p==head)
                    {
                        q->next=head;
                        head=q;
                    }
                    else
                    {
                        q->next=p;
                        pre->next=q;
                    }
                    break;
                }
            }
            if(p==NULL)
                pre->next=q;
            //每一个元素插入完成之后都要将p和pre指向头结点,从头开始比较插入
            p=pre=head;
            q=tmp;
        }
        return head;
    }
    void createList(ListNode *&head)
    {
        ListNode *p=NULL;
        int i=0;
        int arr[10]= {3,4,1,4,6,3,7,2,1,10};
        for(i=0; i<10; i++)
        {
            if(head==NULL)
            {
                head=new ListNode(arr[i]);
                if(head==NULL)
                    return;
            }
            else
            {
                p=new ListNode(arr[i]);
                p->next=head;
                head=p;
            }
        }
    }
};
int main()
{
    Solution s;
    ListNode *L=NULL;
    s.createList(L);
    ListNode *head=L;
    while(head)
    {
        cout<<head->val<<" ";
        head=head->next;
    }
    cout<<endl;
    L=s.insertionSortList(L);
    while(L)
    {
        cout<<L->val<<" ";
        L=L->next;
    }
}

运行结果:

相关文章
|
4月前
|
C++ 容器
【C++】STL容器——探究List与Vector在使用sort函数排序的区别(14)
【C++】STL容器——探究List与Vector在使用sort函数排序的区别(14)
|
9月前
|
算法 C++
SGI-STL源码剖析之list::sort()
SGI-STL源码剖析之list::sort()
76 0
LeetCode 148. Sort List
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
58 0
LeetCode 148. Sort List
|
Python
Python 使用列表的sort()进行多级排序实例演示,list的sort()排序方法使用详解,python3中sort()的cmp自定义排序方法,sort()的逆序、倒叙排序方法
Python 使用列表的sort()进行多级排序实例演示,list的sort()排序方法使用详解,python3中sort()的cmp自定义排序方法,sort()的逆序、倒叙排序方法
687 0
Python 使用列表的sort()进行多级排序实例演示,list的sort()排序方法使用详解,python3中sort()的cmp自定义排序方法,sort()的逆序、倒叙排序方法
|
移动开发 C#
[C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例
[转]http://www.cnblogs.com/dooroo/archive/2012/12/11/2813858.html public class student { //构造函数 public student(int _number, ...
1345 0