Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad344d1

Browse files
author
Krishnal Ciccolella
committed
Create 25-Reverse-Nodes-in-K-Group.js
1 parentc0b8ee5 commitad344d1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} head
10+
*@param {number} k
11+
*@return {ListNode}
12+
*/
13+
varreverseKGroup=function(head,k){
14+
letdummy=newListNode(0,head);
15+
letgroupPrev=dummy;
16+
17+
while(true){
18+
letkth=getKth(groupPrev,k);
19+
if(!kth){
20+
break;
21+
}
22+
23+
letgroupNext=kth.next;
24+
25+
// reverse group
26+
letprev=kth.next;
27+
letcurr=groupPrev.next;
28+
29+
while(curr!==groupNext){
30+
lettemp=curr.next;
31+
curr.next=prev;
32+
prev=curr;
33+
curr=temp;
34+
}
35+
36+
lettemp=groupPrev.next;
37+
groupPrev.next=kth;
38+
groupPrev=temp;
39+
}
40+
41+
returndummy.next;
42+
};
43+
44+
functiongetKth(curr,k){
45+
while(curr&&k>0){
46+
curr=curr.next;
47+
k-=1;
48+
}
49+
50+
returncurr;
51+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp