Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
Example 1:
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
Example 2:
Input: head = [], val = 1
Output: []
Example 3:
Input: head = [7,7,7,7], val = 7
Output: []
Constraints:
func removeElements(head *ListNode, val int) *ListNode {
if head == nil {
return head
}
for head.Val == val && head.Next != nil {
head = head.Next
}
if head.Val == val {
return nil
}
prev, node := head, head.Next
for node != nil && prev != nil {
if node.Val == val {
prev.Next, node = node.Next, node.Next
} else {
prev, node = node, node.Next
}
}
return head
}
func removeElements(head *ListNode, val int) *ListNode {
var fwd, curr *ListNode
for(head != nil && head.Val == val) {
head = head.Next
}
curr = head
for (curr != nil) {
fwd = curr.Next
if fwd != nil && fwd.Val == val {
curr.Next = fwd.Next
} else {
curr = curr.Next
}
}
return head
}
func removeElements(head *ListNode, val int) *ListNode {
nh := &ListNode{Next: head}
for c := nh; c != nil && c.Next != nil; c = c.Next {
for c.Next != nil && c.Next.Val == val {
c.Next = c.Next.Next
}
}
return nh.Next
}
func removeElements(head *ListNode, val int) *ListNode {
for head != nil && head.Val == val{
head = head.Next
}
var current, prev *ListNode = head, nil
for current != nil{
if current.Val == val{
prev.Next=current.Next
}else{
prev = current
}
current = current.Next
}
return head
}
func removeElements(head *ListNode, val int) *ListNode {
tmp := &ListNode {
Next: head,
}
prev := tmp
cur := head
for cur != nil {
if cur.Val == val {
prev.Next = cur.Next
} else {
prev = cur
}
cur = cur.Next
}
return tmp.Next
}
func removeElements(head *ListNode, val int) *ListNode {
dummy := &ListNode{Next:head}
fast, slow := head, dummy
for fast!=nil {
if fast.Val == val {
for fast!=nil && fast.Val == val { // \U0001f608
fast = fast.Next
}
slow.Next = fast
// slow = fast // \U0001f608 ❌ slow应永远滞后fast一步
} else {
fast = fast.Next
slow = slow.Next
}
}
return dummy.Next
}
func removeElements(head *ListNode, val int) *ListNode {
var p,lastGood *ListNode
if head == nil { return nil}
p = head
for p != nil && p.Val == val {
p = p.Next
}
head = p
lastGood = head
for p != nil {
if p.Val == val {
lastGood.Next = p.Next
} else {
lastGood = p
}
p = p.Next
}
return head
}
func removeElements(head *ListNode, val int) *ListNode {
var previous *ListNode
for current := head; current!= nil; current = current.Next {
if current.Val == val {
if current == head {
head = head.Next
} else {
previous.Next = current.Next
}
// current=head
continue
}
previous = current
}
return head
}
func removeElements(head *ListNode, val int) *ListNode {
var prev *ListNode
temp := head
for ; temp != nil ; {
if temp.Val == val && temp == head {
head = head.Next
temp = head
}else if temp.Val == val {
if temp.Next != nil {
prev.Next = temp.Next
temp = temp.Next
}else {
temp = nil
prev.Next = nil
}
}else{
temp = temp.Next
if prev == nil {
prev = head
}else{
prev = prev.Next
}
}
}
return head
}
func removeElements(head *ListNode, val int) *ListNode {
if head == nil {
return nil
}
nn := new(ListNode)
curr := head
prev := nn
nn.Next = head
for curr != nil {
if curr.Val == val {
prev.Next = curr.Next
} else {
prev = prev.Next
}
curr = curr.Next
}
return nn.Next
}
func removeElements(h *ListNode, val int) *ListNode {
var prev *ListNode = nil
result := h
for h != nil {
if h.Val == val && prev == nil {
result = result.Next
h = h.Next
continue
}
if h.Val == val && prev != nil {
prev.Next = h.Next
}
if h.Val != val {
prev = h
}
h = h.Next
}
return result
}
func removeElements(list *ListNode, val int) *ListNode {
if list == nil {
return nil
}
var head *ListNode
var temp *ListNode
for list != nil {
if list.Val == val {
list = list.Next
continue
}
if head == nil {
head = &ListNode{Val: list.Val}
temp = head
list = list.Next
continue
}
temp.Next = &ListNode{Val: list.Val}
temp = temp.Next
list = list.Next
}
return head
}
[Go] Linked List - Palindrome Linked List (0) | 2022.05.09 |
---|---|
[Go] Linked List - Odd Even Linked List (0) | 2022.05.06 |
[Go] Linked List - Reverse Linked List (0) | 2022.05.02 |
[Go] Linked List - Summary - Two-Pointer in Linked List from Java to Go (0) | 2022.05.01 |
[Go] Linked List - Remove Nth Node From End of List (0) | 2022.05.01 |
댓글 영역