Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0025 (#3902)
Browse files Browse the repository at this point in the history
No.0025.Reverse Nodes in k-Group
  • Loading branch information
yanglbme authored Dec 30, 2024
1 parent 2f30bab commit 0bb033f
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 682 deletions.
396 changes: 162 additions & 234 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/README.md

Large diffs are not rendered by default.

396 changes: 162 additions & 234 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.cpp
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;
}
};
53 changes: 27 additions & 26 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,47 @@
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* public ListNode(int val = 0, ListNode next = null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseKGroup(ListNode head, int k) {
ListNode dummy = new ListNode(0, head);
ListNode pre = dummy, cur = dummy;
while (cur.next != null)
{
for (int i = 0; i < k && cur != null; ++i)
{
var dummy = new ListNode(0);
dummy.next = head;
var pre = dummy;

while (pre != null) {
var cur = pre;
for (int i = 0; i < k; i++) {
if (cur.next == null) {
return dummy.next;
}
cur = cur.next;
}
if (cur == null)
{
return dummy.next;
}
ListNode t = cur.next;

var node = pre.next;
var nxt = cur.next;
cur.next = null;
ListNode start = pre.next;
pre.next = ReverseList(start);
start.next = t;
pre = start;
cur = pre;
pre.next = Reverse(node);
node.next = nxt;
pre = node;
}

return dummy.next;
}

private ListNode ReverseList(ListNode head) {
ListNode pre = null, p = head;
while (p != null)
{
ListNode q = p.next;
p.next = pre;
pre = p;
p = q;
private ListNode Reverse(ListNode head) {
ListNode prev = null;
var cur = head;
while (cur != null) {
var nxt = cur.next;
cur.next = prev;
prev = cur;
cur = nxt;
}
return pre;
return prev;
}
}
38 changes: 23 additions & 15 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,36 @@
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
var dummy *ListNode = &ListNode{}
p, cur := dummy, head
for cur != nil {
start := cur
dummy := &ListNode{Next: head}
pre := dummy

for pre != nil {
cur := pre
for i := 0; i < k; i++ {
cur = cur.Next
if cur == nil {
p.Next = start
return dummy.Next
}
cur = cur.Next
}
p.Next, p = reverse(start, cur), start

node := pre.Next
nxt := cur.Next
cur.Next = nil
pre.Next = reverse(node)
node.Next = nxt
pre = node
}
return dummy.Next
}

func reverse(start, end *ListNode) *ListNode {
var pre *ListNode = nil
for start != end {
tmp := start.Next
start.Next, pre = pre, start
start = tmp
func reverse(head *ListNode) *ListNode {
var dummy *ListNode
cur := head
for cur != nil {
nxt := cur.Next
cur.Next = dummy
dummy = cur
cur = nxt
}
return pre
}
return dummy
}
44 changes: 23 additions & 21 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,35 @@
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dummy = new ListNode(0, head);
ListNode pre = dummy, cur = dummy;
while (cur.next != null) {
for (int i = 0; i < k && cur != null; ++i) {
dummy.next = head;
ListNode pre = dummy;
while (pre != null) {
ListNode cur = pre;
for (int i = 0; i < k; i++) {
cur = cur.next;
if (cur == null) {
return dummy.next;
}
}
if (cur == null) {
return dummy.next;
}
ListNode t = cur.next;
ListNode node = pre.next;
ListNode nxt = cur.next;
cur.next = null;
ListNode start = pre.next;
pre.next = reverseList(start);
start.next = t;
pre = start;
cur = pre;
pre.next = reverse(node);
node.next = nxt;
pre = node;
}
return dummy.next;
}

private ListNode reverseList(ListNode head) {
ListNode pre = null, p = head;
while (p != null) {
ListNode q = p.next;
p.next = pre;
pre = p;
p = q;
private ListNode reverse(ListNode head) {
ListNode dummy = new ListNode();
ListNode cur = head;
while (cur != null) {
ListNode nxt = cur.next;
cur.next = dummy.next;
dummy.next = cur;
cur = nxt;
}
return pre;
return dummy.next;
}
}
}
78 changes: 43 additions & 35 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.php
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;
}
}
36 changes: 18 additions & 18 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
def reverseList(head):
pre, p = None, head
while p:
q = p.next
p.next = pre
pre = p
p = q
return pre
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
def reverse(head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
cur = head
while cur:
nxt = cur.next
cur.next = dummy.next
dummy.next = cur
cur = nxt
return dummy.next

dummy = ListNode(next=head)
pre = cur = dummy
while cur.next:
dummy = pre = ListNode(next=head)
while pre:
cur = pre
for _ in range(k):
cur = cur.next
if cur is None:
return dummy.next
t = cur.next
node = pre.next
nxt = cur.next
cur.next = None
start = pre.next
pre.next = reverseList(start)
start.next = t
pre = start
cur = pre
pre.next = reverse(node)
node.next = nxt
pre = node
return dummy.next
Loading

0 comments on commit 0bb033f

Please sign in to comment.