Skip to content

Commit

Permalink
Create: 19-Remove-Nth-Node-From-End-of-List.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 25, 2022
1 parent 95c7b70 commit a2cd670
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions go/19-Remove-Nth-Node-From-End-of-List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(head *ListNode, n int) *ListNode {
if head == nil {
return head
}

slow, fast := head, head
for n > 0 {
fast = fast.Next
n--
}

if fast == nil {
return slow.Next
}

for fast.Next != nil {
slow = slow.Next
fast = fast.Next
}

slow.Next = slow.Next.Next
return head
}

0 comments on commit a2cd670

Please sign in to comment.