halfrost / LeetCode-Go

✅ Solutions to LeetCode by Go, 100% test coverage, runtime beats 100% / LeetCode 题解

Home Page:https://books.halfrost.com/leetcode

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A problem in 707. Design Linked List

Shiwei-Luo opened this issue · comments

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.

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.