Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit846e6c3

Browse files
authored
Update 0148-sort-list.java
1 parent64df5a2 commit846e6c3

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

‎java/0148-sort-list.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Applying Merge Sort on the Linked List
2-
31
/**
42
* Definition for singly-linked list.
53
* public class ListNode {
@@ -59,3 +57,25 @@ public ListNode sortList(ListNode head) {
5957
returnmerge(left,right);
6058
}
6159
}
60+
61+
// Using a Heap to sort the list
62+
classSolution {
63+
publicListNodesortList(ListNodehead) {
64+
if(head ==null ||head.next ==null){
65+
returnhead;
66+
}
67+
PriorityQueue<Integer>queue =newPriorityQueue<>();
68+
ListNodetemp =head;
69+
while(temp.next!=null){
70+
queue.add(temp.val);
71+
temp =temp.next;
72+
}
73+
queue.add(temp.val);
74+
temp =head;
75+
while(!queue.isEmpty()){
76+
temp.val =queue.poll();
77+
temp =temp.next;
78+
}
79+
returnhead;
80+
}
81+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp