---
# System prepended metadata

title: 2046. Sort Linked List Already Sorted Using Absolute Values
tags: [Leetcode, Linked List, Medium]

---

# 2046. Sort Linked List Already Sorted Using Absolute Values
###### tags: `Leetcode` `Medium` `Linked List`
Link: https://leetcode.com/problems/sort-linked-list-already-sorted-using-absolute-values/description/
## 思路
按常理建一个dummyNode放在head前面
然后遍历整个linkedlist 每次遇到负值 就把这个node挪到dummynode的后面
例如```[0,2,-5,5,10,-10]->[dummy, 0,2,-5,5,10,-10]->[dummy,-5,0,2,5,10,-10]->[dummy,-10,-5,0,2,5,10]```
要注意如果第一个数是负数 其实是不用动它的 如果line7不加```curr!=head```的条件 那么当只有一个node[-1]时就会错
## Code
```java=
class Solution {
    public ListNode sortLinkedList(ListNode head) {
        ListNode dummyNode = new ListNode();
        dummyNode.next = head;
        ListNode curr = head, prev = dummyNode;
        while(curr!=null){
            if(curr.val<0 && curr!=head){
                ListNode currNext = curr.next;
                ListNode dummyNext = dummyNode.next;
                dummyNode.next = curr;
                curr.next = dummyNext;
                prev.next = currNext;
                curr = currNext;
            }
            else{
                prev = curr;
                curr = curr.next;
            }
        }
        return dummyNode.next;
    }
}
```