Remove Linked List Elements &&remove-duplicates-from-sorted-list

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

样例

Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

这道题的算法其实很简单,如果遇到要删除的元素,跳过,直接找到下一个非目标元素;无奈学渣的我对链表竟然已经到了如此退化的题目,所以也研究了一些时间。

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     /**
11      * @param head a ListNode
12      * @param val an integer
13      * @return a ListNode
14      */
15     public ListNode removeElements(ListNode head, int val) {
16         // Write your code here
17         ListNode helpnode = new ListNode (0);
18         helpnode.next = head;
19         ListNode p = helpnode;
20         while(p.next != null){
21             if(p.next.val == val){
22                 p.next = p.next.next;
23             }
24             else {
25                 p = p.next;
26             }
27         }
28         return helpnode.next;
29     }
30 }

拓展题:

删除排序链表中的重复元素

容易 删除排序链表中的重复元素查看运行结果

给定一个排序链表,删除所有重复的元素每个元素只留下一个

样例

给出1->1->2->null,返回 1->2->null

给出1->1->2->3->3->null,返回 1->2->3->null

 1 /**
 2  * Definition for ListNode
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     /**
14      * @param ListNode head is the head of the linked list
15      * @return: ListNode head of linked list
16      */
17     public static ListNode deleteDuplicates(ListNode head) {
18         ListNode p = new ListNode(0);
19         p.next = head;
20         if(head == null )
21            return null;
22         while (head.next != null){
23             if (head.next.val == head.val ){
24                  head.next = head.next.next;
25              }else{
26              head = head.next;
27              }
28         }
29         return  p.next;
30     }
31 }
时间: 2024-11-07 10:13:06

Remove Linked List Elements &&remove-duplicates-from-sorted-list的相关文章

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 题目大意: 删除链表中全部的目标元素. 代码如下:

[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 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_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 =

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

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 203 Remove Linked List Elements

要求 在链表中删除值为val的所有节点 示例 如 1->2->3->4->5->6->NULL,要求删除值为6的节点 返回1->2->3->4->5->NULL 思路 删除一般元素(包括最后一个元素) 删除第一个元素 实现 常规思路 1 #include <iostream> 2 using namespace std; 3 4 struct ListNode { 5 int val; 6 ListNode *next; 7 L

[Linked List]Remove Linked List Elements

Total Accepted: 43183 Total Submissions: 160460 Difficulty: Easy 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 --&

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 1 /** 2 * Definition for singly-linked list. 3 * struct Lis

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