mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
new solution!
This commit is contained in:
20
Medium/remove-nth-node-from-end-of-list/solution.go
Normal file
20
Medium/remove-nth-node-from-end-of-list/solution.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
* type ListNode struct {
|
||||||
|
* Val int
|
||||||
|
* Next *ListNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
||||||
|
cap := &ListNode{0, head}
|
||||||
|
lcap, rcap := cap, cap
|
||||||
|
for i := 0; i < n + 1; i++ {
|
||||||
|
rcap = rcap.Next
|
||||||
|
}
|
||||||
|
for rcap != nil {
|
||||||
|
rcap = rcap.Next
|
||||||
|
lcap = lcap.Next
|
||||||
|
}
|
||||||
|
lcap.Next = lcap.Next.Next
|
||||||
|
return cap.Next
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user