# [LeetCode] 148. Sort List, leetcode148.sort

Source: Internet
Author: User

[LeetCode] 148. Sort List, leetcode148.sort
[Question]

Sort a linked list in O (n log n) time using constant space complexity.

[Analysis]

A single-chain table is suitable for Merge Sorting, and a two-way linked list is suitable for quick sorting. The Merge Two Sorted Lists method can be reused.

[Code]

`/********************************** Date: * Author: SJF0115 * Subject: 148. sort List * Source: https://oj.leetcode.com/problems/sort-list/* result: AC * Source: LeetCode * blog: * *********************************/# include <iostream> # include <climits> using namespace std; struct ListNode {int val; ListNode * next; ListNode (int x): val (x), next (NULL) {}}; class Solution {public: listNode * sortList (ListNode * head) {// fault tolerance Processing if (head = NULL | head-> next = NULL) {return head;} // if // define two pointers ListNode * fast = head, * slow = head; // slow pointer to find the intermediate node while (fast-> next! = NULL & fast-> next! = NULL) {// fast pointer one-time two-step fast = fast-> next; // slow pointer one-step slow = slow-> next ;} /// while // disconnect from the intermediate node fast = slow-> next; slow-> next = NULL; // sort the ListNode * left = sortList (head ); // ListNode * right = sortList (fast); // return mergeTwoLists (left, right);} private: ListNode * mergeTwoLists (ListNode * l1, listNode * l2) {ListNode * head = new ListNode (-1); for (ListNode * p = head; l1! = NULL | l2! = NULL; p = p-> next) {int val1 = (l1 = NULL )? INT_MAX: l1-> val; int val2 = (l2 = NULL )? INT_MAX: l2-> val; if (val1 <= val2) {p-> next = l1; l1 = l1-> next ;} // if else {p-> next = l2; l2 = l2-> next;} // else} // for return head-> next ;}; int main () {Solution solution; int A [] = {8, 3, 10, 5, 11, 1, 4}; // linked list ListNode * head = new ListNode (A [0]); listNode * p = head; for (int I = 1; I <7; I ++) {ListNode * node = new ListNode (A [I]); p-> next = node; p = node;} // for head = solution. sortList (head); // output p = head; while (p) {cout <p-> val <""; p = p-> next ;} // while cout <endl; return 0 ;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.