-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortList.cpp
More file actions
46 lines (37 loc) · 861 Bytes
/
SortList.cpp
File metadata and controls
46 lines (37 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// SortList.cpp
// LeetCode
//
// Created by gongshang on 15/10/22.
// Copyright (c) 2015年 ninerec. All rights reserved.
//
/**
* 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) {
int length = count_size(head);
ListNode virtual_head = new ListNode(0);
virtual_head->next = head;
int block = 1;
while (block < length) {
block *= 2;
}
return virtual_head->next;
}
private:
int count_size(ListNode* head) {
int total = 0;
while (head != NULL) {
total++;
head = head->next;
}
return total;
}
};