Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7427571

Browse files
commit go 203
1 parent a841704 commit 7427571

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎go/203.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import "fmt"
4+
5+
/**
6+
* LC#203 移除链表元素 Remove Linked List Elements
7+
* Link:https://leetcode-cn.com/problems/remove-linked-list-elements/
8+
* 思路1(optimum solution 最优解):初始化一个 sentinel 遍历检查链表 head 链表,判断 tmp.val == val 就放弃当前引用,指向下一个引用 简单粗暴。从官方答案来看这应该也是最优解了
9+
*/
10+
type ListNode struct {
11+
Val int
12+
Next *ListNode
13+
}
14+
15+
func removeElements(head *ListNode, val int) *ListNode {
16+
sentinel := &ListNode{0, head}
17+
for tmp := sentinel; tmp.Next != nil; {
18+
if tmp.Next.Val == val {
19+
tmp.Next = tmp.Next.Next
20+
} else {
21+
tmp = tmp.Next
22+
}
23+
}
24+
return sentinel.Next
25+
}
26+
27+
func main() {
28+
29+
// build a simple linked
30+
n7 := ListNode{6, nil}
31+
n6 := ListNode{5, &n7}
32+
n5 := ListNode{4, &n6}
33+
n4 := ListNode{3, &n5}
34+
n3 := ListNode{6, &n4}
35+
n2 := ListNode{2, &n3}
36+
n1 := ListNode{1, &n2}
37+
cur := removeElements(&n1, 6)
38+
39+
// Output Linked ...
40+
for cur != nil {
41+
fmt.Printf("cur %v , val %d \n", cur, cur.Val)
42+
cur = cur.Next
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /