From e6d511dc9d948690bdc77d474ca346c2ac90451a Mon Sep 17 00:00:00 2001 From: Ramy hakam Date: Sun, 28 Nov 2021 23:09:32 +0100 Subject: [PATCH] Reverse Linked List II --- LinkedList/ReverseLinkedListII.php | 39 +++++++++++++++++++ .../InputFiles/ReverseLinkedListIITest.json | 10 +++++ Tests/LinkedList/ReverseLinkedListIITest.php | 26 +++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 LinkedList/ReverseLinkedListII.php create mode 100644 Tests/Helper/InputFiles/ReverseLinkedListIITest.json create mode 100644 Tests/LinkedList/ReverseLinkedListIITest.php diff --git a/LinkedList/ReverseLinkedListII.php b/LinkedList/ReverseLinkedListII.php new file mode 100644 index 0000000..6f67e0b --- /dev/null +++ b/LinkedList/ReverseLinkedListII.php @@ -0,0 +1,39 @@ +next; + } + + $p2 = $p1->next; + $p3 = $p2; + $reverted = null; + + for ($i = 0; $i <= ($right - $left); $i++) { + $tmp = $p2->next; + $p2->next = $reverted; + $reverted = $p2; + $p2 = $tmp; + } + + $p1->next = $reverted; + $p3->next = $p2; + return $sentinel->next; + } +} \ No newline at end of file diff --git a/Tests/Helper/InputFiles/ReverseLinkedListIITest.json b/Tests/Helper/InputFiles/ReverseLinkedListIITest.json new file mode 100644 index 0000000..1f02eac --- /dev/null +++ b/Tests/Helper/InputFiles/ReverseLinkedListIITest.json @@ -0,0 +1,10 @@ +{ + "testCase1": { + "expectedResult":[1,4,3,2,5], + "input": [[1,2,3,4,5],2,4] + }, + "testCase2": { + "expectedResult":[5], + "input": [[5],1,1] + } +} \ No newline at end of file diff --git a/Tests/LinkedList/ReverseLinkedListIITest.php b/Tests/LinkedList/ReverseLinkedListIITest.php new file mode 100644 index 0000000..a1fba52 --- /dev/null +++ b/Tests/LinkedList/ReverseLinkedListIITest.php @@ -0,0 +1,26 @@ +convertFromArray($inputData[0]); + self::assertEmpty(array_diff($expectedResult, + $this->convertToArray($reversList->reverseBetween($list,$inputData[1],$inputData[2])))); + } +} \ No newline at end of file