-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsol.py
33 lines (32 loc) · 838 Bytes
/
sol.py
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# Two Pass
class Solution:
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
curr = head
count = 0
while curr is not None:
count+=1
curr = curr.next
bad_node = count-n+1
curr = head
ll = ListNode(None)
trav = ll
for enum in range(bad_node-1):
trav.next = ListNode(curr.val)
trav = trav.next
curr = curr.next
curr = curr.next
while curr is not None:
trav.next = ListNode(curr.val)
trav = trav.next
curr = curr.next
return ll.next