diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/README.md b/solution/0000-0099/0019.Remove Nth Node From End of List/README.md index 310c885f7de9a..e9931cdde2d49 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/README.md +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/README.md @@ -278,6 +278,38 @@ var removeNthFromEnd = function (head, n) { }; ``` +#### Swift + +````swift +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ +class Solution { + func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { + let dummy = ListNode(0) + dummy.next = head + var fast: ListNode? = dummy + var slow: ListNode? = dummy + for _ in 0..val = $val; -# $this->next = $next; -# } -# } - +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ class Solution { /** * @param ListNode $head - * @param int $n + * @param Integer $n * @return ListNode */ - function removeNthFromEnd($head, $n) { - $dummy = new ListNode(0); - $dummy->next = $head; - - $first = $dummy; - $second = $dummy; - - for ($i = 0; $i <= $n; $i++) { - $second = $second->next; + $dummy = new ListNode(0, $head); + $fast = $slow = $dummy; + for ($i = 0; $i < $n; $i++) { + $fast = $fast->next; } - - while ($second != null) { - $first = $first->next; - $second = $second->next; + while ($fast->next !== null) { + $fast = $fast->next; + $slow = $slow->next; } - - $first->next = $first->next->next; - + $slow->next = $slow->next->next; return $dummy->next; } } @@ -358,3 +381,4 @@ class Solution { +```` diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md b/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md index c2e89d64db00c..6c85e356c3109 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md @@ -275,6 +275,38 @@ var removeNthFromEnd = function (head, n) { }; ``` +#### Swift + +````swift +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ +class Solution { + func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { + let dummy = ListNode(0) + dummy.next = head + var fast: ListNode? = dummy + var slow: ListNode? = dummy + for _ in 0..val = $val; -# $this->next = $next; -# } -# } - +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ class Solution { /** * @param ListNode $head - * @param int $n + * @param Integer $n * @return ListNode */ - function removeNthFromEnd($head, $n) { - $dummy = new ListNode(0); - $dummy->next = $head; - - $first = $dummy; - $second = $dummy; - - for ($i = 0; $i <= $n; $i++) { - $second = $second->next; + $dummy = new ListNode(0, $head); + $fast = $slow = $dummy; + for ($i = 0; $i < $n; $i++) { + $fast = $fast->next; } - - while ($second != null) { - $first = $first->next; - $second = $second->next; + while ($fast->next !== null) { + $fast = $fast->next; + $slow = $slow->next; } - - $first->next = $first->next->next; - + $slow->next = $slow->next->next; return $dummy->next; } } @@ -355,3 +378,4 @@ class Solution { +```` diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.php b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.php index a03530a2304f4..004a0f484e7ad 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.php +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.php @@ -1,40 +1,31 @@ -# Definition for singly-linked list. -# class ListNode { -# public $val; -# public $next; - -# public function __construct($val = 0, $next = null) -# { -# $this->val = $val; -# $this->next = $next; -# } -# } - +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ class Solution { /** * @param ListNode $head - * @param int $n + * @param Integer $n * @return ListNode */ - function removeNthFromEnd($head, $n) { - $dummy = new ListNode(0); - $dummy->next = $head; - - $first = $dummy; - $second = $dummy; - - for ($i = 0; $i <= $n; $i++) { - $second = $second->next; + $dummy = new ListNode(0, $head); + $fast = $slow = $dummy; + for ($i = 0; $i < $n; $i++) { + $fast = $fast->next; } - - while ($second != null) { - $first = $first->next; - $second = $second->next; + while ($fast->next !== null) { + $fast = $fast->next; + $slow = $slow->next; } - - $first->next = $first->next->next; - + $slow->next = $slow->next->next; return $dummy->next; } } diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.swift b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.swift new file mode 100644 index 0000000000000..310d4aa25aa78 --- /dev/null +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.swift @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ +class Solution { + func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { + let dummy = ListNode(0) + dummy.next = head + var fast: ListNode? = dummy + var slow: ListNode? = dummy + for _ in 0..