算法提高之LeetCode刷题Swift in LeetCodeLeetCode solutions

Swift 删除链表中的节点 - LeetCode

2018-11-27  本文已影响6人  韦弦Zhy
LeetCode

题目: 删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

现有一个链表 -- head = [4,5,1,9],它可以表示为:

  4 -> 5 -> 1 -> 9

示例1:

输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例2:

输入: head = [4,5,1,9], node = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

说明:

代码:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */
class Solution {
  func deleteNode(_ node? : ListNode) {
      node.val = node.next.val;
      node.next = node.next.next;
  }
}
上一篇 下一篇

猜你喜欢

热点阅读