상세 컨텐츠

본문 제목

[Go] Linked List - Remove Linked List Elements

Go/Leet Code

by Gopythor 2022. 5. 5. 23:40

본문

728x90
반응형

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:

  • The number of nodes in the list is in the range [0, 104].
  • 1 <= Node.val <= 50
  • 0 <= val <= 50

Mycode

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
}

sample 0 ms submission

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
}
  • I think this is simple way.

sample 1 ms submission

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
}

sample 2 ms submission

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
}

sample 3 ms submission

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
}

sample 4 ms submission

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
}

sample 5 ms submission

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
}

sample 6 ms submission

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
}

sample 4600 KB submission

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
}

sample 4700 KB submission

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
}

sample 4800 KB submission

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
    
}

sample 4900 KB submission

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
}
728x90
반응형

관련글 더보기

댓글 영역