Skip to content

Latest commit

 

History

History
235 lines (196 loc) · 5.91 KB

File metadata and controls

235 lines (196 loc) · 5.91 KB
comments difficulty edit_url tags
true
中等
链表
双指针
排序

English Version

题目描述

给你一个链表的头结点 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]
解释:
这个链表已经是升序的了。

 

提示:

  • 链表节点数的范围是 [1, 105].
  • -5000 <= Node.val <= 5000
  • head 是根据结点绝对值升序排列好的链表.

 

进阶:
  • 你可以在O(n)的时间复杂度之内解决这个问题吗?

解法

方法一:头插法

我们先默认第一个点已经排序完毕,然后从第二个点开始,遇到值为负数的节点,采用头插法;非负数,则继续往下遍历即可。

时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$

Python3

# 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

Java

/**
 * 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;
    }
}

C++

/**
 * 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;
    }
};

Go

/**
 * 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
}

TypeScript

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function sortLinkedList(head: ListNode | null): ListNode | null {
    let [prev, curr] = [head, head.next];
    while (curr !== null) {
        if (curr.val < 0) {
            const t = curr.next;
            prev.next = t;
            curr.next = head;
            head = curr;
            curr = t;
        } else {
            [prev, curr] = [curr, curr.next];
        }
    }
    return head;
}