Title: #148 - Sort List - LeetCode JavaScript Solutions
Open Graph Title: #148 - Sort List - LeetCode JavaScript Solutions
X Title: #148 - Sort List - LeetCode JavaScript Solutions
Description: Given the head of a linked list, return the list after sorting it in ascending order. ...
Open Graph Description: Given the head of a linked list, return the list after sorting it in ascending order. ...
X Description: Given the head of a linked list, return the list after sorting it in ascending order. ...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/sort-list
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/sort-list","name":"#148 - Sort List - LeetCode JavaScript Solutions","description":"Given the head of a linked list, return the list after sorting it in ascending order. ...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar sortList = function(head) {\n if (!head || !head.next) return head;\n let fast = head;\n let slow = head;\n while (fast.next && fast.next.next) {\n fast = fast.next.next;\n slow = slow.next;\n }\n const middle = slow.next;\n slow.next = null;\n return mergeList(sortList(head), sortList(middle));\n};\n\nfunction mergeList(a, b) {\n const ordered = new ListNode(-1);\n let list = ordered;\n\n while (a && b) {\n list.next = a.val < b.val ? a : b;\n list = list.next;\n if (a.val < b.val) {\n a = a.next;\n } else {\n b = b.next;\n }\n }\n\n list.next = a ?? b;\n return ordered.next;\n}","keywords":"LeetCode 148, #148 - Sort List, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/sort-list |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/sort-list/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0148-sort-list.js |
| Sorting | https://leetcodejavascript.com/tags/sorting |
| Two Pointers | https://leetcodejavascript.com/tags/two-pointers |
| Linked List | https://leetcodejavascript.com/tags/linked-list |
| Divide and Conquer | https://leetcodejavascript.com/tags/divide-and-conquer |
| Merge Sort | https://leetcodejavascript.com/tags/merge-sort |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow