返回介绍

solution / 2000-2099 / 2046.Sort Linked List Already Sorted Using Absolute Values / README_EN

发布于 2024-06-17 01:03:11 字数 4822 浏览 0 评论 0 收藏 0

2046. Sort Linked List Already Sorted Using Absolute Values

中文文档

Description

Given the head of a singly linked list that is sorted in non-decreasing order using the absolute values of its nodes, return _the list sorted in non-decreasing order using the actual values of its nodes_.

 

Example 1:

Input: head = [0,2,-5,5,10,-10]
Output: [-10,-5,0,2,5,10]
Explanation:
The list sorted in non-descending order using the absolute values of the nodes is [0,2,-5,5,10,-10].
The list sorted in non-descending order using the actual values is [-10,-5,0,2,5,10].

Example 2:

Input: head = [0,1,2]
Output: [0,1,2]
Explanation:
The linked list is already sorted in non-decreasing order.

Example 3:

Input: head = [1]
Output: [1]
Explanation:
The linked list is already sorted in non-decreasing order.

 

Constraints:

  • The number of nodes in the list is the range [1, 105].
  • -5000 <= Node.val <= 5000
  • head is sorted in non-decreasing order using the absolute value of its nodes.

 

Follow up:

  • Can you think of a solution with O(n) time complexity?

Solutions

Solution 1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文