Skip to content

Commit fdedc76

Browse files
committed
19 still confused
1 parent 62983fb commit fdedc76

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
3636
|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/#/description)| [Python](./linkedlist/deleteDuplicates.py) | _O(n)_| _O(1)_ | Easy | ||
3737
|148|[Sort List](https://leetcode.com/problems/sort-list/#/description)| [Python](./linkedlist/sortList.py) | _O(nlogn)_| _O(1)_ | Medium | ||
3838
|61|[Rotate List](https://leetcode.com/problems/rotate-list/#/description)| [Python](./linkedlist/rotateRight.py) | _O(n)_| _O(1)_ | Medium | ||
39+
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/#/description)| [Python](./linkedlist/removeNthFromEnd.py) | _O(n)_| _O(1)_ | Medium | ||

linkedlist/removeNthFromEnd.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def removeNthFromEnd(self, head, n):
9+
slow = fast = head
10+
for _ in xrange(n):
11+
fast = fast.next
12+
13+
# 我本来的代码没有15-16行,可以给我解释下这个edge case干了什么么
14+
# 论坛里面都设置了dummynode,这题为什么要设置dummynode
15+
# 先去玩狼人杀,回来继续研究
16+
if not fast:
17+
return head.next
18+
while fast.next:
19+
slow = slow.next
20+
fast = fast.next
21+
slow.next = slow.next.next
22+
return head

0 commit comments

Comments
 (0)