Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0019 (#3901)
Browse files Browse the repository at this point in the history
No.0019.Remove Nth Node From End of List
  • Loading branch information
yanglbme authored Dec 30, 2024
1 parent 66e1a30 commit 2f30bab
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 87 deletions.
82 changes: 53 additions & 29 deletions solution/0000-0099/0019.Remove Nth Node From End of List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n {
fast = fast?.next
}
while fast?.next != nil {
fast = fast?.next
slow = slow?.next
}
slow?.next = slow?.next?.next
return dummy.next
}
}
```

#### Ruby

```rb
Expand Down Expand Up @@ -311,43 +343,34 @@ end
#### PHP

```php
# 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;
}
}
Expand All @@ -358,3 +381,4 @@ class Solution {
<!-- solution:end -->

<!-- problem:end -->
````
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n {
fast = fast?.next
}
while fast?.next != nil {
fast = fast?.next
slow = slow?.next
}
slow?.next = slow?.next?.next
return dummy.next
}
}
```

#### Ruby

```rb
Expand Down Expand Up @@ -308,43 +340,34 @@ end
#### PHP

```php
# 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;
}
}
Expand All @@ -355,3 +378,4 @@ class Solution {
<!-- solution:end -->

<!-- problem:end -->
````
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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..<n {
fast = fast?.next
}
while fast?.next != nil {
fast = fast?.next
slow = slow?.next
}
slow?.next = slow?.next?.next
return dummy.next
}
}

0 comments on commit 2f30bab

Please sign in to comment.