- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0019-remove-nth-node-from-end-of-list.rb
55 lines (46 loc) · 1.02 KB
/
0019-remove-nth-node-from-end-of-list.rb
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
50
51
52
53
54
55
# frozen_string_literal: true
# 19. Remove Nth Node From End of List
# https://leetcode.com/problems/remove-nth-node-from-end-of-list
=begin
Given the head of a linked list, remove the nth node from the end of the list and return its head.
### Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
### Example 2:
Input: head = [1], n = 1
Output: []
### Example 3:
Input: head = [1,2], n = 1
Output: [1]
### Constraints:
The number of nodes in the list is sz.
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
=end
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# @param {ListNode} head
# @param {Integer} n
# @return {ListNode}
defremove_nth_from_end(head,n)
dummy=ListNode.new(0)
dummy.next=head
slow=dummy
fast=dummy
(n + 1).timesdo
fast=fast.next
end
whilefast
slow=slow.next
fast=fast.next
end
slow.next=slow.next.next
dummy.next
end