mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
fix/19: clean up code
This commit is contained in:
@ -17,31 +17,18 @@ type ListNode = structures.ListNode
|
||||
|
||||
// 解法一
|
||||
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
var fast, slow *ListNode
|
||||
fast = head
|
||||
slow = head
|
||||
step := 0
|
||||
for i := 0; i < n; i++ {
|
||||
// n maybe much larger than length of linklist
|
||||
if fast.Next == nil && step < n-1 {
|
||||
return head
|
||||
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
|
||||
step++
|
||||
}
|
||||
if fast == nil {
|
||||
head = head.Next
|
||||
return head
|
||||
}
|
||||
for fast.Next != nil {
|
||||
fast = fast.Next
|
||||
slow = slow.Next
|
||||
}
|
||||
slow.Next = slow.Next.Next
|
||||
return head
|
||||
preSlow.Next = slow.Next
|
||||
return dummyHead.Next
|
||||
}
|
||||
|
||||
// 解法二
|
||||
|
Reference in New Issue
Block a user