-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://leetcode-cn.com/problems/partition-list/
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ArrayToListNode } from "../reverse-linked-list/ArrayToListNode.ts"; | ||
import { ListNode } from "../reverse-linked-list/ListNode.ts"; | ||
import { ListNodeToArray } from "../reverse-linked-list/ListNodeToArray.ts"; | ||
|
||
export default function partition( | ||
head: ListNode | null, | ||
x: number, | ||
): ListNode | null { | ||
if (!head) return head; | ||
if (!head.next) return head; | ||
const array = ListNodeToArray(head); | ||
const small = array.filter((a) => a < x); | ||
const big = array.filter((a) => a >= x); | ||
return ArrayToListNode([...small, ...big]); | ||
} | ||
// function ListNodeToArray(list: ListNode | null): Array<number> { | ||
// if (list === null) { | ||
// return []; | ||
// } | ||
// const array: Array<number> = []; | ||
// let temp: ListNode | null = list; | ||
// while (temp) { | ||
// array.push(temp.val); | ||
// temp = temp.next; | ||
// } | ||
// return array; | ||
// } | ||
// function ArrayToListNode(array: Array<number>): ListNode | null { | ||
// if (array.length === 0) { | ||
// return null; | ||
// } | ||
// const list = new ListNode(array[0]); | ||
// let cur = list; | ||
// for (let i = 1; i < array.length; i++) { | ||
// const v = array[i]; | ||
// const l = new ListNode(v); | ||
// cur.next = l; | ||
// cur = cur.next; | ||
// } | ||
// // array.slice(1).reduce((p, v) => { | ||
// // const l = new ListNode(v); | ||
// // p.next = l; | ||
// // return l; | ||
// // }, list); | ||
// return list; | ||
// } |