-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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.0025 (#3902)
No.0025.Reverse Nodes in k-Group
- Loading branch information
Showing
11 changed files
with
530 additions
and
682 deletions.
There are no files selected for viewing
396 changes: 162 additions & 234 deletions
396
solution/0000-0099/0025.Reverse Nodes in k-Group/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
396 changes: 162 additions & 234 deletions
396
solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.cpp
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,48 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* reverseKGroup(ListNode* head, int k) { | ||
ListNode* dummy = new ListNode(0, head); | ||
ListNode* pre = dummy; | ||
|
||
while (pre != nullptr) { | ||
ListNode* cur = pre; | ||
for (int i = 0; i < k; i++) { | ||
cur = cur->next; | ||
if (cur == nullptr) { | ||
return dummy->next; | ||
} | ||
} | ||
|
||
ListNode* node = pre->next; | ||
ListNode* nxt = cur->next; | ||
cur->next = nullptr; | ||
pre->next = reverse(node); | ||
node->next = nxt; | ||
pre = node; | ||
} | ||
return dummy->next; | ||
} | ||
|
||
private: | ||
ListNode* reverse(ListNode* head) { | ||
ListNode* dummy = new ListNode(); | ||
ListNode* cur = head; | ||
while (cur != nullptr) { | ||
ListNode* nxt = cur->next; | ||
cur->next = dummy->next; | ||
dummy->next = cur; | ||
cur = nxt; | ||
} | ||
return dummy->next; | ||
} | ||
}; |
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
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
78 changes: 43 additions & 35 deletions
78
solution/0000-0099/0025.Reverse Nodes in k-Group/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,51 +1,59 @@ | ||
# 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 $k | ||
* @param Integer $k | ||
* @return ListNode | ||
*/ | ||
|
||
function reverseKGroup($head, $k) { | ||
$dummy = new ListNode(0); | ||
$dummy->next = $head; | ||
$prevGroupTail = $dummy; | ||
$pre = $dummy; | ||
|
||
while ($head !== null) { | ||
$count = 0; | ||
$groupHead = $head; | ||
$groupTail = $head; | ||
|
||
while ($count < $k && $head !== null) { | ||
$head = $head->next; | ||
$count++; | ||
} | ||
if ($count < $k) { | ||
$prevGroupTail->next = $groupHead; | ||
break; | ||
} | ||
|
||
$prev = null; | ||
while ($pre !== null) { | ||
$cur = $pre; | ||
for ($i = 0; $i < $k; $i++) { | ||
$next = $groupHead->next; | ||
$groupHead->next = $prev; | ||
$prev = $groupHead; | ||
$groupHead = $next; | ||
if ($cur->next === null) { | ||
return $dummy->next; | ||
} | ||
$cur = $cur->next; | ||
} | ||
$prevGroupTail->next = $prev; | ||
$prevGroupTail = $groupTail; | ||
|
||
$node = $pre->next; | ||
$nxt = $cur->next; | ||
$cur->next = null; | ||
$pre->next = $this->reverse($node); | ||
$node->next = $nxt; | ||
$pre = $node; | ||
} | ||
|
||
return $dummy->next; | ||
} | ||
|
||
/** | ||
* Helper function to reverse a linked list. | ||
* @param ListNode $head | ||
* @return ListNode | ||
*/ | ||
function reverse($head) { | ||
$prev = null; | ||
$cur = $head; | ||
while ($cur !== null) { | ||
$nxt = $cur->next; | ||
$cur->next = $prev; | ||
$prev = $cur; | ||
$cur = $nxt; | ||
} | ||
return $prev; | ||
} | ||
} |
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
Oops, something went wrong.