-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Hakam\LeetCodePhp\LinkedList; | ||
|
||
class RemoveNthNodeFromEndOfList | ||
{ | ||
/** | ||
* @param ListNode $head | ||
* @param Integer $n | ||
* @return ListNode | ||
*/ | ||
public function removeNthFromEnd(ListNode $head, int $n) : ?ListNode | ||
{ | ||
$dummy = new ListNode(0); | ||
$dummy->next = $head; | ||
$copyHead = $head; | ||
$length = 0; | ||
while ($copyHead !== null) { | ||
$copyHead = $copyHead->next; | ||
++$length; | ||
} | ||
$length -= $n; | ||
$copyHead = $dummy; | ||
while ($length > 0) { | ||
$length--; | ||
$copyHead = $copyHead->next; | ||
} | ||
|
||
$copyHead->next = $copyHead->next->next; | ||
|
||
return $dummy->next; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Tests/Helper/InputFiles/RemoveNthNodeFromEndOfListTest.json
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,14 @@ | ||
{ | ||
"testCase1": { | ||
"expectedResult":[2], | ||
"input": [[1,2],2] | ||
}, | ||
"testCase2": { | ||
"expectedResult":[1,2,3,5], | ||
"input": [[1,2,3,4,5],2] | ||
}, | ||
"testCase3": { | ||
"expectedResult":[], | ||
"input": [[1],1] | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace Hakam\LeetCodePhp\Tests\LinkedList; | ||
|
||
|
||
use Hakam\LeetCodePhp\LinkedList\RemoveNthNodeFromEndOfList; | ||
use Hakam\LeetCodePhp\Tests\Helper\LinkedListHelperTrait; | ||
use Hakam\LeetCodePhp\Tests\Helper\MainTest; | ||
|
||
/** | ||
* @covers \Hakam\LeetCodePhp\LinkedList\RemoveNthNodeFromEndOfList | ||
*/ | ||
class RemoveNthNodeFromEndOfListTest extends MainTest | ||
{ | ||
use LinkedListHelperTrait; | ||
|
||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testWithDataList($expectedResult, $inputData): void | ||
{ | ||
$list = $this->convertFromArray($inputData[0]); | ||
$remover = new RemoveNthNodeFromEndOfList(); | ||
self::assertEmpty(array_diff($expectedResult, | ||
$this->convertToArray( | ||
$remover->removeNthFromEnd($list,$inputData[1])) | ||
)); | ||
} | ||
} |