diff --git a/src/main/java/leetcode/editor/cn/RemoveLinkedListElements.java b/src/main/java/leetcode/editor/cn/RemoveLinkedListElements.java new file mode 100644 index 0000000..b9877ac --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveLinkedListElements.java @@ -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) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/RemoveLinkedListElements.md b/src/main/java/leetcode/editor/cn/RemoveLinkedListElements.md new file mode 100644 index 0000000..c5c062c --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveLinkedListElements.md @@ -0,0 +1,34 @@ +给你一个链表的头节点 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
+输出:[]
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 链表
  • \n
  • 👍 613
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/RemoveLinkedListElements144362264.txt b/src/main/java/leetcode/editor/cn/RemoveLinkedListElements144362264.txt new file mode 100644 index 0000000..8c04d09 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveLinkedListElements144362264.txt @@ -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