/**
* 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) {
if(head == NULL){
return head;
}
if(head->next == NULL){
return head;
}
if(head->next->next == NULL){
if(head->next->val > head->val){
ListNode *tmp = head->next;
heed->next->next = head;
head->next = NULL;
return tmp;
}
else{
return head;
}
}
ListNode* fast, *slow;
fast = slow = head;
while(fast->next){
fast = fast->next->next;
slow = slow->next;
if(fast == NULL){
break;
}
}
/*if the length of a linked list is even number, such as 2 * l, then return the l-th node;
else if the length of a linked list is odd number, such as 2 * l + 1, then return (l + 1)-th node
*/
return slow;
}
};
找到链表的中点
最新推荐文章于 2024-01-16 20:04:42 发布