C实现 LeetCode->Merge Two Sorted Lists (双指针大法)
2015-06-18 12:02
337 查看
Sort a linked list in O(n log n)
time using constant space complexity.
* 将单链表
排序 O(n lg n)
//
// SortList.c
// Algorithms
//
// Created by TTc on 15/6/18.
// Copyright (c) 2015年 TTc. All rights reserved.
//
/**
* Sort a linked list in O(n log n) time using constant space complexity.
* 将单链表 排序
*/
#include "SortList.h"
struct ListNode {
int val;
struct ListNode *next;
};
/**
* 双指针大法,创建两个 指针(首尾指针 向中间扫描)
*/
struct ListNode*
sortList(struct ListNode* head) {
if(!head) return NULL;
struct ListNode *start = head;
struct ListNode *end = head;
int length = 0;
while(start) {
length++;
start = start->next;
}
if(length == 1) return head;
int mid = length/2; //计算结点个数,将链表分开。
//将链表分开
struct ListNode *tmpNode = NULL;
for(int i = 0; i < mid && end != NULL; i++) {
if(i == mid - 1) tmpNode = end;
end = end->next;
}
if(tmpNode != NULL) tmpNode->next = NULL;
//分别对两段排序
start = head;
start = sortList(start);
end = sortList(end);
struct ListNode *newhead = NULL;//新创建的list头节点
struct ListNode *newtemp = NULL;//中间遍历接收变量
while(start && end) { //合并
if(start->val <= end->val) {
if(!newhead) newtemp = newhead = start;
else{
newtemp->next = start;
newtemp = start;
}
start = start->next;
}else{
if(!newhead) newhead = newtemp = end;
else{
newtemp->next = end;
newtemp = end;
}
end = end->next;
}
}
if(start) newtemp->next = start;
if(end) newtemp->next = end;
return newhead;
}
time using constant space complexity.
* 将单链表
排序 O(n lg n)
//
// SortList.c
// Algorithms
//
// Created by TTc on 15/6/18.
// Copyright (c) 2015年 TTc. All rights reserved.
//
/**
* Sort a linked list in O(n log n) time using constant space complexity.
* 将单链表 排序
*/
#include "SortList.h"
struct ListNode {
int val;
struct ListNode *next;
};
/**
* 双指针大法,创建两个 指针(首尾指针 向中间扫描)
*/
struct ListNode*
sortList(struct ListNode* head) {
if(!head) return NULL;
struct ListNode *start = head;
struct ListNode *end = head;
int length = 0;
while(start) {
length++;
start = start->next;
}
if(length == 1) return head;
int mid = length/2; //计算结点个数,将链表分开。
//将链表分开
struct ListNode *tmpNode = NULL;
for(int i = 0; i < mid && end != NULL; i++) {
if(i == mid - 1) tmpNode = end;
end = end->next;
}
if(tmpNode != NULL) tmpNode->next = NULL;
//分别对两段排序
start = head;
start = sortList(start);
end = sortList(end);
struct ListNode *newhead = NULL;//新创建的list头节点
struct ListNode *newtemp = NULL;//中间遍历接收变量
while(start && end) { //合并
if(start->val <= end->val) {
if(!newhead) newtemp = newhead = start;
else{
newtemp->next = start;
newtemp = start;
}
start = start->next;
}else{
if(!newhead) newhead = newtemp = end;
else{
newtemp->next = end;
newtemp = end;
}
end = end->next;
}
}
if(start) newtemp->next = start;
if(end) newtemp->next = end;
return newhead;
}
相关文章推荐
- Solr5 DataImport 处理1对多关系
- Android之带渐变动画的欢迎界面
- 数据结构——顺序表(c++)
- Left Menu抽屉效果ScrollView姿态共存冲突
- #leetcode#Triangle
- php常用表单验证类用法实例
- 关于css定位
- Mybatis 连接oracle数据库配置文件
- 常用窗口函数
- Centos Lnmp 增加数据库备份
- java nio 网络框架实现
- TCP与UDP区别之见
- 九度1035 -树 - 找出直系亲属
- ORACLE触发特定的解释
- Linux中比较
- Linux下脚本自动监控Tomcat服务
- CXF调用webservice日记
- 【深度学习系列3】 Mariana CNN并行框架与图像识别
- 学习用的工具
- 我们测量的人生规划 你走到其中那一步了呢