leetcode Remove Linked List Elements 203

Remove Linked List Elements

Total
Accepted: 11291 Total
Submissions: 42824

Remove all elements from a linked list of integers that have value val.

Example

Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6

Return: 1 --> 2 --> 3 --> 4 --> 5

Credits:

Special thanks to for adding this problem and creating all test cases.

翻译:移除一个整型链表中值为val的所有元素

解题思路:类似指针,一个前节点 preNode, 一个当前节点curNode ,当遇到值为val的节点时, 前节点preNode的下一节点直接指向当前节点 curNode的下一节点,即跳过值为val的节点   preNode.next=curNode.next 。

代码如下

/**
 * 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)
    {
		ListNode temp = new ListNode(0);
		temp.next=head;
		ListNode preNode=temp;//设置前节点为空
		ListNode curNode=head;//设置当前节点为头节点
		while (curNode!=null)
		{
			if (curNode.val==val)
			{
				preNode.next=curNode.next;
			}
			else
			{
				preNode=preNode.next;
			}
			curNode=curNode.next;
		}
		return temp.next;
    }
}
时间: 2024-10-19 19:36:09

leetcode Remove Linked List Elements 203的相关文章

LeetCode Remove Linked List Elements 删除链表元素

题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 1

LeetCode:Remove Linked List Elements

Problem: Remove all elements from a linked list of integers that have value val; Example Given :1->2->6->3->4->5->6 val=6 Return:1->2->3->4->5Solution: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * in

[LeetCode] Remove Linked List Elements 移除链表元素

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 Credits:Special thanks to @mithmatt for adding this probl

leetcode: Remove Linked List Elements(java)

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 Credits: Special thanks to @mithmatt for adding this prob

leetCode 203. Remove Linked List Elements 链表

203. Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题目大意: 删除链表中全部的目标元素. 代码如下:

203. Remove Linked List Elements - LeetCode

Question 203.?Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 Java实现: public ListNode removeElements(ListNode head, int val) { ListNode cur = head; while (cur != null) { if (cur.next != null && cur.next.val ==

[LeetCode][JavaScript]Remove Linked List Elements

Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 https://leetcode.com/problems/r

leetcode_203题——Remove Linked List Elements(链表)

Remove Linked List Elements Total Accepted: 8053 Total Submissions: 29898My Submissions Question Solution Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val =

Java [Leetcode 203]Remove Linked List Elements

题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 解题思路: 链表操作. 代码如下: /** * Definition for singly-linked