Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0019 #3901

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading