insertion sort list leetcode(插入排序 链表)
来源:互联网 发布:动漫导航源码 编辑:程序博客网 时间:2024/04/28 18:22
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <iostream>
#include <stdio.h>
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) {
int flag=1;
ListNode* p1=NULL;
ListNode* p2=NULL;
ListNode* p=NULL;
ListNode* temp=NULL;
ListNode* head1=NULL;
if(head->val>head->next->val)
{ p=head->next;
head->next=head->next->next;
p->next=head;
head=p; //保证头两个节点有序
}
head1=head->next; //p的前一个
for(p=head->next->next;p!=NULL;) //从第三个节点开始
{ temp=p;
for(p1=head;p1->next!=p;p1=p1->next)
{
if(p1->val<temp->val&&p1->next->val>=temp->val)
{
p2=p->next;
head1->next=p->next;
p->next=p1->next;
p1->next=p;
p=p2;
flag=0;
break;
}
else if(head->val>=temp->val)
{
p2=p->next;
head1->next=p->next;
p->next=head;
head=p;
p=p2;
flag=0;
break;
}
}
if(flag==1)
{
head1=head1->next;
p=p->next;
}
}
return head;
}
};
int main() {
Solution solution;
//int A[] = {2,4,7,9};
//int B[] = {5,6,4};
int i;
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 5;i >0;i--){
node = new ListNode(i);
// node->exp = A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
/* pre = head2;
for(int i = 2;i < 5;i++){
node = new ListNode(i,i+1);
// node->val = B[i];
node->next = NULL;
pre->next = node;
pre = node;
}*/
head = solution.insertionSortList(head1->next);
while(head != NULL){
// printf("%d %d ",head->coef,head->exp);
cout<<head->val<<' ';
head = head->next;
}
//i=(head1->val);
//cout<<i;
return 0;
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include <iostream>
#include <stdio.h>
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) {
int flag=1;
ListNode* p1=NULL;
ListNode* p2=NULL;
ListNode* p=NULL;
ListNode* temp=NULL;
ListNode* head1=NULL;
if(head->val>head->next->val)
{ p=head->next;
head->next=head->next->next;
p->next=head;
head=p; //保证头两个节点有序
}
head1=head->next; //p的前一个
for(p=head->next->next;p!=NULL;) //从第三个节点开始
{ temp=p;
for(p1=head;p1->next!=p;p1=p1->next)
{
if(p1->val<temp->val&&p1->next->val>=temp->val)
{
p2=p->next;
head1->next=p->next;
p->next=p1->next;
p1->next=p;
p=p2;
flag=0;
break;
}
else if(head->val>=temp->val)
{
p2=p->next;
head1->next=p->next;
p->next=head;
head=p;
p=p2;
flag=0;
break;
}
}
if(flag==1)
{
head1=head1->next;
p=p->next;
}
}
return head;
}
};
int main() {
Solution solution;
//int A[] = {2,4,7,9};
//int B[] = {5,6,4};
int i;
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 5;i >0;i--){
node = new ListNode(i);
// node->exp = A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
/* pre = head2;
for(int i = 2;i < 5;i++){
node = new ListNode(i,i+1);
// node->val = B[i];
node->next = NULL;
pre->next = node;
pre = node;
}*/
head = solution.insertionSortList(head1->next);
while(head != NULL){
// printf("%d %d ",head->coef,head->exp);
cout<<head->val<<' ';
head = head->next;
}
//i=(head1->val);
//cout<<i;
return 0;
}
花一天千辛万苦的编出来了结果说超时了。 不过对于链表排序, 首先要记住被删除节点的头结点和尾节点, 节点其实就是地址。 可以有两个节点的next为同一个节点
0 0
- insertion sort list leetcode(插入排序 链表)
- LeetCode Insertion Sort List(单链表插入排序)
- 147.leetcode Insertion Sort List(medium)[链表 插入排序]
- LeetCode | Insertion Sort List(插入法排序链表)
- LeetCode 147 Insertion Sort List(链表插入排序)
- Leetcode 147 Insertion Sort List(插入排序列表)
- 147. Insertion Sort List(插入排序)
- 插入排序(Insertion Sort List)
- Insertion Sort List 链表插入排序@LeetCode
- LeetCode:Insertion Sort List//链表插入排序
- leetcode Insertion Sort List(链表插入排序)
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- Leetcode Insertion Sort List 插入排序链表
- LeetCode Insertion Sort List 链表的插入排序
- leetcode 147. Insertion Sort List-链表插入排序
- [LeetCode] Insertion Sort List 单向链表插入排序
- LeetCode-147. Insertion Sort List (JAVA)链表插入排序
- leetcode 147. Insertion Sort List 链表插入排序
- Search for a Range
- 埃氏筛素数法
- iOS开发之统计xcode代码行数
- Java中的容器详细讲解学习
- 第十二周项目四(1):点、圆的关系
- insertion sort list leetcode(插入排序 链表)
- 多线程
- 大数据在营销中的应用
- java 保存对象到文件,读取文件中的对象
- 最近,与学习
- 第十一周 项目3-点类派生直线类
- 第十周项目三 职工的薪水
- 几何对象的结构和类
- python深入笔记--特殊方法\上下文管理\__dict__\property()