[Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements

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

Example:

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


删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

72ms
 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
14         guard head != nil else { return nil }
15
16         var head = head
17         while head != nil, head!.val == val {
18             head = head?.next
19         }
20
21         var prev = head
22         var current = head?.next
23
24         while let curr = current {
25             if curr.val == val {
26                 prev?.next = curr.next
27                 current = curr.next
28                 continue
29             }
30             prev = curr
31             current = curr.next
32         }
33
34         return head
35     }
36 }


76ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
14         guard let head = head else { return nil }
15         head.next = removeElements(head.next, val)
16         return head.val == val ? head.next : head
17     }
18 }


80ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
14
15         var dummy = ListNode(-1)
16         dummy.next = head
17         var cur = dummy
18         while cur.next != nil {
19             if cur.next!.val == val {
20                 cur.next = cur.next!.next
21             }
22             else{
23                 cur = cur.next!
24             }
25         }
26         return dummy.next
27     }
28 }


96ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12
13 class Solution {
14     func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
15         guard let _ = head else {
16             return nil
17         }
18
19         //在表头添加一个哨兵节点
20         let fakeNode = ListNode(NSNotFound)
21         fakeNode.next = head
22         var prev:ListNode = fakeNode
23         var current:ListNode? = head
24         while let tmp = current {
25             if tmp.val != val {
26                 prev.next = tmp
27                 prev = prev.next!
28             }
29             current = tmp.next
30         }
31
32         if prev.next != nil {
33             prev.next = nil
34         }
35         return fakeNode.next
36     }
37 }

原文地址:https://www.cnblogs.com/strengthen/p/10184259.html

时间: 2024-08-29 18:36:08

[Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements的相关文章

leetcode 203. 移除链表元素(Remove Linked List Elements)

目录 题目描述: 示例: 解法: 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), nex

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 题意:移除链表中指定val的元素 注意:考虑删除节点在尾部,以及连续删除两个相邻节点的情况 /** * Definit

leetcode203. 移除链表元素

删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements(struct List

[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 删除链表元素

题意:移除链表中元素值为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 链表

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

3. 无重复字符的最长子串 141. 环形链表 171. Excel表列序号 203. 移除链表元素

3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3.示例 2: 输入: "bbbbb"输出: 1解释: 因为无重复字符的最长子串是 "b",所以其长度为 1.示例 3: 输入: "pwwkew"输出: 3解释: 因为无重复字符的最长子串是 "

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