Skip to content

Commit

Permalink
https://leetcode-cn.com/problems/partition-list/
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Apr 22, 2022
1 parent 1b11da3 commit 9907869
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ https://leetcode-cn.com/problems/hPov7L/

https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/

https://leetcode-cn.com/problems/partition-list/

#### 安装教程

1. 安装`deno`
Expand Down
46 changes: 46 additions & 0 deletions partition-list/index.ts
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;
// }

0 comments on commit 9907869

Please sign in to comment.