#include
using namespace std;
#include
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *sortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
// 归并排序不解释
if (head == NULL) {
return head;
}
//求得list 的长度,以便获得一半
int len = 0;
ListNode *cur = head;
while (cur) {
cur = cur->next;
len++;
}
if (len == 1) { //只有一个节点
return head;
}
int mid_len = len / 2 - 1; //找到前一个节点
cur = head;
int index = 0;
while (index < mid_len) {
cur = cur->next;
index++;
}
ListNode *first_head = head;
ListNode *second_head = cur->next;
cur->next = NULL; //第一个链表最后为空
//递归调用
first_head = sortList(first_head);
second_head = sortList(second_head);
//合并链表
ListNode *result_head = NULL;
ListNode *result_tail = NULL;
while (first_head && second_head) {
if (first_head->val < second_head->val) {
if (result_head == NULL) {
result_head = first_head;
}
else {
result_tail->next = first_head;
}
result_tail = first_head;
first_head = first_head->next;
}
else {
if (result_head == NULL) {
result_head = second_head;
}
else {
result_tail->next = second_head;
}
result_tail = second_head;
second_head = second_head->next;
}
}
while (first_head) { //处理第一个链表剩余部分,由于一定是一半处,所以result_head不为空,不用判断了
result_tail->next = first_head;
result_tail = first_head;
first_head = first_head->next;
}
while (second_head) { //处理第二个链表剩余部分,由于一定是一半处,所以result_head不为空,不用判断了
result_tail->next = second_head;
result_tail = second_head;
second_head = second_head->next;
}
return result_head;
}
};
int main() {
Solution so;
ListNode l1(1), l2(2), l3(3), l4(4), l5(5);
l1.next = &l3;
l3.next = &l5;
l5.next = &l4;
l4.next = &l2;
ListNode *result = so.sortList(&l1);
while (result) {
cout << result->val << " ";
result = result->next;
}
}
多谢网友(
1楼
wwwwwwking)的评价,参考STL源码,实现了链表排序的非递归版本,根据leetcode最后的测试结果显示,递归版本300ms,非递归版本280ms,相差不是很大。而且非递归理解起来比较困难,最好是实际手动操作五六个元素。代码如下,欢迎指正。
#include
using namespace std;
#include
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *merge(ListNode *first_head, ListNode *second_head) {
ListNode *result_head = NULL;
ListNode *result_tail = NULL;
while (first_head && second_head) {
if (first_head->val < second_head->val) {
if (result_head == NULL) {
result_head = first_head;
}
else {
result_tail->next = first_head;
}
result_tail = first_head;
first_head = first_head->next;
}
else {
if (result_head == NULL) {
result_head = second_head;
}
else {
result_tail->next = second_head;
}
result_tail = second_head;
second_head = second_head->next;
}
}
while (first_head) { //处理第一个链表剩余部分
if (result_head == NULL) {
result_head = first_head;
}
else {
result_tail->next = first_head;
}
result_tail = first_head;
first_head = first_head->next;
}
while (second_head) { //处理第二个链表剩余部分
if (result_head == NULL) {
result_head = second_head;
}
else {
result_tail->next = second_head;
}
result_tail = second_head;
second_head = second_head->next;
}
return result_head;
}
ListNode *sortList(ListNode *head) {
//280ms
if (head == NULL || head->next == NULL) { //空节点或者一个节点
return head;
}
ListNode *carry = NULL;
struct Node {
ListNode *head;
};
Node counter[64] = {0}; //临时链表节点
for (int i = 0; i < 64; i++) {
counter[i].head = NULL;
}
ListNode *cur = head;
int fill = 0;
while (cur) {
ListNode *next = cur->next;
cur->next = NULL; //每次只有一个节点
carry = cur;
int i = 0;
while (i < fill && counter[i].head != NULL) {
counter[i].head = merge(counter[i].head, carry);
// fill the counter[i].head = NULL //如果满足2的n次方,向前移动指针
carry = counter[i].head;
counter[i].head = NULL;
i++;
}
counter[i].head = carry; //将carry赋值给最高的counter
carry = NULL;
if (i == fill) { //满足2的n次方
fill++;
}
cur = next; //下一个节点
}
for (int i = 1; i < fill; i++) {
counter[i].head = merge(counter[i].head, counter[i - 1].head);
}
return counter[fill - 1].head;
}
};
int main() {
Solution so;
ListNode l1(1), l2(2), l3(3), l4(4), l5(5);
l1.next = &l3;
l3.next = &l5;
l5.next = &l4;
l4.next = &l2;
//1 3 5 4 2
ListNode *result = so.sortList(&l1);
while (result) {
cout << result->val << " ";
result = result->next;
}
}