列表

详情


2046. 给按照绝对值排序的链表排序

给你一个链表的头结点 head ,这个链表是根据结点的绝对值进行升序排序, 返回重新根据节点的值进行升序排序的链表。

 

示例 1:

输入: head = [0,2,-5,5,10,-10]
输出: [-10,-5,0,2,5,10]
解释:
根据结点的值的绝对值排序的链表是 [0,2,-5,5,10,-10].
根据结点的值排序的链表是 [-10,-5,0,2,5,10].

示例 2:

输入: head = [0,1,2]
输出: [0,1,2]
解释:
这个链表已经是升序的了。

示例 3:

输入: head = [1]
输出: [1]
解释:
这个链表已经是升序的了。

 

提示:

 

进阶:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* sortLinkedList(ListNode* head) { } };

java 解法, 执行用时: 53 ms, 内存消耗: 61 MB, 提交时间: 2023-10-17 16:58:47

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
// 最小堆
class Solution {
    public ListNode sortLinkedList(ListNode head) {
        PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b)-> a.val - b.val);
        ListNode p = head, dummy = new ListNode(0, head);
        while (p!=null){
            pq.add(p);
            p = p.next;
        }
        p = dummy;
        while (!pq.isEmpty()){
            p.next = pq.poll();
            p = p.next;
        }
        p.next = null;
        return dummy.next;
    }
}

java 解法, 执行用时: 2 ms, 内存消耗: 60 MB, 提交时间: 2023-10-17 16:58:07

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortLinkedList(ListNode head) {
        ListNode prev = head, curr = head.next;
        while (curr != null) {
            if (curr.val < 0) {
                ListNode t = curr.next;
                prev.next = t;
                curr.next = head;
                head = curr;
                curr = t;
            } else {
                prev = curr;
                curr = curr.next;
            }
        }
        return head;
    }
}

cpp 解法, 执行用时: 184 ms, 内存消耗: 90.9 MB, 提交时间: 2023-10-17 16:57:35

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* sortLinkedList(ListNode* head) {
        ListNode* prev = head;
        ListNode* curr = head->next;
        while (curr)
        {
            if (curr->val < 0)
            {
                auto t = curr->next;
                prev->next = t;
                curr->next = head;
                head = curr;
                curr = t;
            }
            else
            {
                prev = curr;
                curr = curr->next;
            }
        }
        return head;
    }
};

golang 解法, 执行用时: 80 ms, 内存消耗: 8.3 MB, 提交时间: 2023-10-17 16:57:15

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func sortLinkedList(head *ListNode) *ListNode {
	prev, curr := head, head.Next
	for curr != nil {
		if curr.Val < 0 {
			t := curr.Next
			prev.Next = t
			curr.Next = head
			head = curr
			curr = t
		} else {
			prev, curr = curr, curr.Next
		}
	}
	return head
}

python3 解法, 执行用时: 332 ms, 内存消耗: 58.3 MB, 提交时间: 2023-10-17 16:57:00

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
'''
头插法,默认第一个节点已经排好序,从第二个点开始,遇到负数,头插法,非负继续往下
'''
class Solution:
    def sortLinkedList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev, curr = head, head.next
        while curr:
            if curr.val < 0:
                t = curr.next
                prev.next = t
                curr.next = head
                head = curr
                curr = t
            else:
                prev, curr = curr, curr.next
        return head

上一题