halfrost / LeetCode-Go

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解
https://books.halfrost.com/leetcode
MIT License
32.76k stars 5.68k forks source link

A problem in 707. Design Linked List #215

Closed Shiwei-Luo closed 2 years ago

Shiwei-Luo commented 2 years ago
func (this *MyLinkedList) DeleteAtIndex(index int) {
    cur := this
    for i := 0; cur != nil; i++ {
        if i == index-1 {
            break
        }
        cur = cur.Next
    }
    if cur != nil && cur.Next != nil {
        cur.Next = cur.Next.Next
    }
}

It seems that the case that index equals 0 is not taken into account.

halfrost commented 2 years ago
func (this *MyLinkedList) DeleteAtIndex(index int) {
  cur := this
  for i := 0; cur != nil; i++ {
      if i == index-1 {
          break
      }
      cur = cur.Next
  }
  if cur != nil && cur.Next != nil {
      cur.Next = cur.Next.Next
  }
}

It seems that the case that index equals 0 is not taken into account.

@Shiwei-Luo You are right. I have fixed this bug. Please pull the latest code.