Seems the easiest way to solve this problem , Python Solution.


  • -4
    L

    Actually, I don't like any puzzles related with sort, I know bubble sort, quick sort, merge sort, and I don't want to write them at all, because it's a waste of time, so I directly Use the python sort method,

    Oups, It works!!!. seems the leet code doesn't count the memory limit. HaHa.!!

    class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def sortList(self, head):
        res=[];
        while(head):
            res.append(head.val);
            head=head.next;
        res=sorted(res);
        
        head=ListNode(0);
        resHead=head;
        for v in res:
            resHead.next=ListNode(v);
            resHead=resHead.next;
        
        return head.next;

  • 0
    8

    It is not easy to write a bug-free merge sort algorithm using linked list.
    And I think the goal of this problem is to practices sorting algorithms.
    Using off-the-shelf sorting algorithms to solve this problem is a waste of time, indeed.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.