File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
src/remove-linked-list-elements Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
113113| 190 | [ Reverse Bits] ( https://leetcode.com/problems/reverse-bits/ ) | [ JavaScript] ( ./src/reverse-bits/res.js ) | Easy |
114114| 196 | [ Delete Duplicate Emails] ( https://leetcode.com/problems/delete-duplicate-emails/ ) | [ SQL] ( ./src/delete-duplicate-emails/res.txt ) | Easy |
115115| 197 | [ Rising Temperature] ( https://leetcode.com/problems/rising-temperature/ ) | [ SQL] ( ./src/rising-temperature/res.txt ) | Easy |
116+ | 203 | [ remove-linked-list-elements] ( https://leetcode.com/problems/remove-linked-list-elements/ ) | [ TypeScript] ( ./src/remove-linked-list-elements/res.ts ) | Easy |
116117| 206 | [ Reverse Linked List] ( https://leetcode.com/problems/reverse-linked-list/ ) | [ JavaScript] ( ./src/reverse-linked-list/res.js ) | Easy |
117118| 207 | [ Course Schedule] ( https://leetcode.com/problems/course-schedule/ ) | [ JavaScript] ( ./src/course-schedule/res.js ) | Medium |
118119| 209 | [ Minimum Size Subarray Sum] ( https://leetcode.com/problems/minimum-size-subarray-sum/ ) | [ JavaScript] ( ./src/minimum-size-subarray-sum/res.js ) | Medium |
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * class ListNode {
4+ * val: number
5+ * next: ListNode | null
6+ * constructor(val?: number, next?: ListNode | null) {
7+ * this.val = (val===undefined ? 0 : val)
8+ * this.next = (next===undefined ? null : next)
9+ * }
10+ * }
11+ */
12+
13+ /**
14+ * 递归迭代均可,此法为迭代
15+ * @param head
16+ * @param val
17+ */
18+ function removeElements ( head : ListNode | null , val : number ) : ListNode | null {
19+ let result = head ;
20+ let prevPointer = null ;
21+ let currentPointer = head ;
22+
23+ if ( ! currentPointer ) {
24+ return result ?. val === val ? null : head ;
25+ }
26+
27+ while ( currentPointer ) {
28+ if ( currentPointer . val === val ) {
29+ if ( prevPointer ) {
30+ prevPointer . next = currentPointer . next ;
31+ } else {
32+ result = currentPointer . next ;
33+ }
34+ } else {
35+ prevPointer = currentPointer ;
36+ }
37+
38+ currentPointer = currentPointer . next ;
39+ }
40+
41+ return result ;
42+ } ;
You can’t perform that action at this time.
0 commit comments