From d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa Mon Sep 17 00:00:00 2001
From: jack <0x6A73@pm.me>
Date: Fri, 14 Jun 2024 13:13:15 -0600
Subject: initial

---
 remove_nodes_linked_list.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 remove_nodes_linked_list.c

(limited to 'remove_nodes_linked_list.c')

diff --git a/remove_nodes_linked_list.c b/remove_nodes_linked_list.c
new file mode 100644
index 0000000..0bcae4f
--- /dev/null
+++ b/remove_nodes_linked_list.c
@@ -0,0 +1,59 @@
+// 2487. Remove Nodes From Linked List
+#include "leetcode.h"
+
+/*
+ * given the 'head' of a linked list, remove evey node which has a node with a
+ * greater value anywhere to the right side of it. return the 'head' of the
+ * modified linked list.
+ */
+
+struct ListNode {
+  int val;
+  struct ListNode *next;
+};
+
+int depth(struct ListNode *head) {
+  int n = 0;
+  struct ListNode *tmp = head;
+  while (tmp) {
+    n++;
+    tmp = tmp->next;
+  }
+  return n;
+}
+
+struct ListNode *removeNodes(struct ListNode *head) {
+  int n = depth(head), i = 0;
+  struct ListNode **arr =
+      (struct ListNode **)malloc(n * sizeof(struct ListNode *));
+  struct ListNode *tmp = head;
+  while (tmp) {
+    arr[i] = tmp;
+    i++;
+    tmp = tmp->next;
+  }
+  bool *remove = (bool *)calloc(n, sizeof(bool));
+  int max = arr[n - 1]->val;
+  for (int i = n - 2; i >= 0; i--) {
+    if (arr[i]->val < max)
+      remove[i] = true;
+    else if (arr[i]->val > max)
+      max = arr[i]->val;
+  }
+  struct ListNode *ans;
+  for (int i = 0; i < n; i++) {
+    if (!remove[i]) {
+      ans = arr[i];
+      tmp = ans;
+      for (int j = i + 1; j < n; j++)
+        if (!remove[j]) {
+          tmp->next = arr[j];
+          tmp = tmp->next;
+        }
+      tmp->next = NULL;
+      break;
+    }
+  }
+  free(arr), free(remove);
+  return ans;
+}
-- 
cgit v1.2.3