203--Remove LinkedList Elements
package LinedList;
public class RemoveLinkedListElements {
//解法一:循环
public ListNode removeElements(ListNode head, int val) {
while (head!=null&&head.val==val){
ListNode temp=head;
head=temp.next;
temp.next=null;
}
if (head==null)
return null;
ListNode previous=head;
while (previous.next!=null){
if (previous.next.val==val){
ListNode temp=previous.next;
previous.next=temp.next;
temp.next=null;
}
else{
previous=previous.next;
}
}
return head;
}
//解法二,使用虚拟头节点的循环。
public ListNode removeElements2(ListNode head, int val) {
ListNode dummyHead=new ListNode(-1);
dummyHead.next=head;
ListNode previous=dummyHead;
while (previous.next!=null){
if (previous.next.val==val){
ListNode temp=previous.next;
previous.next=temp.next;
temp.next=null;
} else {
previous=previous.next;
}
}
return dummyHead.next;
}
//解法三:使用递归。
public ListNode removeElements3(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
return head.val==val?head.next:head;
}
}
203--Remove LinkedList Elements的更多相关文章
- LeetCode(203) Remove LinkedList Elements
题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- LeetCode 203:移除链表元素 Remove LinkedList Elements
删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- Java for LeetCode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- [LeetCode] 203. Remove Linked List Elements 解题思路
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
随机推荐
- 语音识别:从 WaveNet 到 Tacotron,再到 RNN-T
从 WaveNet 到 Tacotron,再到 RNN-T 谷歌再获语音识别新进展:利用序列转导来实现多人语音识别和说话人分类 雷锋网 AI 科技评论按:从 WaveNet 到 Tacotron,再到 ...
- leetcode14最长公共前缀
class Solution { public: string longestCommonPrefix(vector<string>& strs) { ) return " ...
- UML系列
UML类图:https://www.cnblogs.com/shindo/p/5579191.html UML用例图:https://www.jianshu.com/p/3cde67aed8e9 UM ...
- 利用nodejs识别二维码内容的方法
const decodeImage = require('jimp').read; const qrcodeReader = require('qrcode-reader'); qrDecode(&q ...
- RaxML使用
1.下载 https://github.com/stamatak/standard-RAxML 2.How many Threads shall I use? 重要的是要知道,RAxML PThrea ...
- CF1217E Sum Queries? (线段树)
完了,前几天才说 edu 的 DEF 都不会,现在打脸了吧 qwq 其实在刚说完这句话 1min 就会了 D,3min 就会了 E 发现,对于大小 \(\ge 3\) 的不平衡集合,它至少有一个大小为 ...
- css设置不可复制
-moz-user-select:none; /* Firefox私有属性 */ -webkit-user-select:none; /* WebKit内核私有属性 */ -ms-user-selec ...
- spring cloud gateway网关启动报错:No qualifying bean of type 'org.springframework.web.reactive.DispatcherHandler'
网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; n ...
- Elasticsearch由浅入深(二)ES基础分布式架构、横向扩容、容错机制
Elasticsearch的基础分布式架构 Elasticsearch对复杂分布式机制的透明隐藏特性 Elasticsearch是一套分布式系统,分布式是为了应对大数据量. Elasticsearch ...
- 检查hdfs块的块-fsck
hadoop集群运行过程中,上下节点是常有的事情,如果下架节点,hdfs存储的块肯定会受到影响. 如何查看当前的hdfs的块的状态 hadoop1.x时候的命令,hadoop2.x也可使用: hado ...