GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/019_Remove_Nth_Node_From_End_of_List.py at master · 539961733/leetcode

Expand file tree

/

019_Remove_Nth_Node_From_End_of_List.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
49 lines (46 loc) · 1.31 KB
/

019_Remove_Nth_Node_From_End_of_List.py

Copy path

File metadata and controls

49 lines (46 loc) · 1.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
# class Solution(object):
# def removeNthFromEnd(self, head, n):
# """
# :type head: ListNode
# :type n: int
# :rtype: ListNode
# """
class Solution(object):
# def removeNthFromEnd(self, head, n):
# # with O(n) space
# index = []
# pos = head
# while pos is not None:
# index.append(pos)
# pos = pos.next
# ls = len(index)
# if n == ls:
# if ls > 1:
# return index[1]
# else:
# return None
# else:
# index_pos = ls - n - 1
# index[index_pos].next = index[index_pos + 1].next
# return head
def removeNthFromEnd(self, head, n):
# https://leetcode.com/discuss/86721/o-n-solution-in-java
if head is None:
return None
slow = fast = head
for i in range(n):
fast = fast.next
if fast is None:
head = head.next
return head
while fast.next is not None:
fast = fast.next
slow = slow.next
curr = slow.next
slow.next = curr.next
return head
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.