mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 12:14:26 +08:00
71 lines
1.1 KiB
Go
71 lines
1.1 KiB
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// ListNode define
|
|
type ListNode = structures.ListNode
|
|
|
|
/**
|
|
* Definition for singly-linked list.
|
|
* type ListNode struct {
|
|
* Val int
|
|
* Next *ListNode
|
|
* }
|
|
*/
|
|
|
|
// 解法一
|
|
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
|
dummyHead := &ListNode{Next: head}
|
|
preSlow, slow, fast := dummyHead, head, head
|
|
for fast != nil {
|
|
if n <= 0 {
|
|
preSlow = slow
|
|
slow = slow.Next
|
|
}
|
|
n--
|
|
fast = fast.Next
|
|
}
|
|
preSlow.Next = slow.Next
|
|
return dummyHead.Next
|
|
}
|
|
|
|
// 解法二
|
|
func removeNthFromEnd1(head *ListNode, n int) *ListNode {
|
|
if head == nil {
|
|
return nil
|
|
}
|
|
if n <= 0 {
|
|
return head
|
|
}
|
|
current := head
|
|
len := 0
|
|
for current != nil {
|
|
len++
|
|
current = current.Next
|
|
}
|
|
if n > len {
|
|
return head
|
|
}
|
|
if n == len {
|
|
current := head
|
|
head = head.Next
|
|
current.Next = nil
|
|
return head
|
|
}
|
|
current = head
|
|
i := 0
|
|
for current != nil {
|
|
if i == len-n-1 {
|
|
deleteNode := current.Next
|
|
current.Next = current.Next.Next
|
|
deleteNode.Next = nil
|
|
break
|
|
}
|
|
i++
|
|
current = current.Next
|
|
}
|
|
return head
|
|
}
|