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
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
/pythonPublic archive

Added remove nth node from end of a linked-list#131

Open
omrawal wants to merge1 commit intoAllAlgorithms:master
base:master
Choose a base branch
Loading
fromomrawal:master
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletionsalgorithms/linkedlist/delete_nth_node_from_end.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
# remove nth node from end
# https://leetcode.com/problems/remove-nth-node-from-end-of-list/

# brute
# create a new linked list without that element
# Time O(n)
# Space O(n)

# optimal


# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
if(head.next == None):
return None
start = ListNode()
start.next = head
slow = fast = start
for i in range(1, n+1):
fast = fast.next
while(fast.next != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return start.next

[8]ページ先頭

©2009-2025 Movatter.jp