-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0019 (#3901)
No.0019.Remove Nth Node From End of List
- Loading branch information
Showing
4 changed files
with
153 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 20 additions & 29 deletions
49
solution/0000-0099/0019.Remove Nth Node From End of List/Solution.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
solution/0000-0099/0019.Remove Nth Node From End of List/Solution.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |