203:移除链表元素
This commit is contained in:
parent
b5a6347f32
commit
242940b75b
@ -0,0 +1,84 @@
|
||||
//给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:head = [1,2,6,3,4,5,6], val = 6
|
||||
//输出:[1,2,3,4,5]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:head = [], val = 1
|
||||
//输出:[]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:head = [7,7,7,7], val = 7
|
||||
//输出:[]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 列表中的节点在范围 [0, 104] 内
|
||||
// 1 <= Node.val <= 50
|
||||
// 0 <= k <= 50
|
||||
//
|
||||
// Related Topics 链表
|
||||
// 👍 613 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//203:移除链表元素
|
||||
class RemoveLinkedListElements{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new RemoveLinkedListElements().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode removeElements(ListNode head, int val) {
|
||||
ListNode temp = head;
|
||||
if (temp == null) {
|
||||
return head;
|
||||
}
|
||||
while (temp.val == val) {
|
||||
head = temp = temp.next;
|
||||
if (temp == null) {
|
||||
return head;
|
||||
}
|
||||
}
|
||||
while (temp != null && temp.next != null) {
|
||||
if (temp.next.val == val) {
|
||||
temp.next = temp.next.next;
|
||||
} else {
|
||||
temp = temp.next;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
34
src/main/java/leetcode/editor/cn/RemoveLinkedListElements.md
Normal file
34
src/main/java/leetcode/editor/cn/RemoveLinkedListElements.md
Normal file
@ -0,0 +1,34 @@
|
||||
给你一个链表的头节点 <code>head</code> 和一个整数 <code>val</code> ,请你删除链表中所有满足 <code>Node.val == val</code> 的节点,并返回 <strong>新的头节点</strong> 。
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg" style="width: 500px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2,6,3,4,5,6], val = 6
|
||||
<strong>输出:</strong>[1,2,3,4,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [], val = 1
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [7,7,7,7], val = 7
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>列表中的节点在范围 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>1 <= Node.val <= 50</code></li>
|
||||
<li><code>0 <= k <= 50</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 613</li><li>👎 0</li></div>
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode removeElements(ListNode head, int val) {
|
||||
ListNode temp = head;
|
||||
if (temp == null) {
|
||||
return head;
|
||||
}
|
||||
while (temp.val == val) {
|
||||
head = temp = temp.next;
|
||||
if (temp == null) {
|
||||
return head;
|
||||
}
|
||||
}
|
||||
while (temp != null && temp.next != null) {
|
||||
if (temp.next.val == val) {
|
||||
temp.next = temp.next.next;
|
||||
} else {
|
||||
temp = temp.next;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
//runtime:1 ms
|
||||
//memory:39.4 MB
|
Loading…
Reference in New Issue
Block a user