File tree Expand file tree Collapse file tree 1 file changed +41
-1
lines changed Expand file tree Collapse file tree 1 file changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ if (!next || next.val !== val) {
5858
5959## 代码
6060
61- - 语言支持:JS,Python
61+ - 语言支持:JS,Python,C++,Java
6262
6363Javascript Code:
6464
@@ -112,6 +112,46 @@ class Solution:
112112 return prev.next
113113```
114114
115+ C++ Code:
116+
117+ ``` cpp
118+ class Solution {
119+ public:
120+ ListNode* removeElements(ListNode* head, int val) {
121+ struct ListNode* dummyHead = new ListNode(0, head);
122+ struct ListNode* temp = dummyHead;
123+ while (temp->next != NULL) {
124+ if (temp->next->val == val) {
125+ temp->next = temp->next->next;
126+ } else {
127+ temp = temp->next;
128+ }
129+ }
130+ return dummyHead->next;
131+ }
132+ };
133+ ```
134+
135+ Java Code:
136+
137+ ```java
138+ class Solution {
139+ public ListNode removeElements(ListNode head, int val) {
140+ ListNode dummyHead = new ListNode(0);
141+ dummyHead.next = head;
142+ ListNode temp = dummyHead;
143+ while (temp.next != null) {
144+ if (temp.next.val == val) {
145+ temp.next = temp.next.next;
146+ } else {
147+ temp = temp.next;
148+ }
149+ }
150+ return dummyHead.next;
151+ }
152+ }
153+ ```
154+
115155** 复杂度分析**
116156
117157- 时间复杂度:$O(N)$
You can’t perform that action at this time.
0 commit comments