Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
31 lines (30 loc) · 995 Bytes

File metadata and controls

31 lines (30 loc) · 995 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
/* if(head==null) return null;
ListNode ln =head;
while(ln.next!=null)
{
if(ln.next.val==val) ln.next=ln.next.next;
else
ln=ln.next;
}
return head.val==val?head.next:head;//都是先不考虑头结点,先考虑头结点下一个节点,最后再考虑头结点*/
ListNode result = head;
if (result == null) {
return null;
} else if (result.val == val) {
return removeElements(result.next, val);
} else {
result.next = removeElements(result.next, val);// 如果头结点的值不等于val,从下一个节点开始迭代,并将其赋值给result.next;
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.