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

Commit756b78f

Browse files
authored
Simply further
Further simplify solution, this also matches the python solution shown in the video.
1 parent849251a commit756b78f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

‎typescript/19-Remove-Nth-Node-From-End-of-List.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,21 @@
1111
*/
1212

1313
functionremoveNthFromEnd(head:ListNode|null,n:number):ListNode|null{
14-
letrunner=head;
15-
letc=0;
16-
17-
while(c!=n&&runner!=null){
18-
c++;
19-
runner=runner.next;
20-
}
21-
22-
if(runner==null){
23-
returnhead.next;
24-
}
25-
26-
lettail=head;
27-
28-
while(runner.next!=null){
29-
runner=runner.next;
30-
tail=tail.next;
31-
}
32-
33-
tail.next=tail.next.next;
34-
35-
returnhead;
36-
}
14+
letdummy:ListNode=newListNode(0,head)
15+
letleft=dummy
16+
letright=head
17+
18+
while(n>0){
19+
right=right.next
20+
n-=1
21+
}
22+
23+
while(right){
24+
left=left.next
25+
right=right.next
26+
}
27+
28+
// delete
29+
left.next=left.next.next
30+
returndummy.next
31+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp