Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. LinkedList

RemoveKFromList

Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k.

Example

  • For l = [3, 1, 2, 3, 4, 5] and k = 3, the output should be solution(l, k) = [1, 2, 4, 5];

  • For l = [1, 2, 3, 4, 5, 6, 7] and k = 10, the output should be solution(l, k) = [1, 2, 3, 4, 5, 6, 7].

Idea

Iteration + in-loop while loop

Iterate from the beginning and eliminate leading k

Iterate the mid-part and eliminate any single k or contiguous k

Code

ListNode<Integer> solution(ListNode<Integer> l, int k) {
    if(l==null){
        return l;
    }
    ListNode<Integer> head = null;
    while(l!=null){
        if(l.value!=k){
            head = l;
            break;
        }
        l = l.next;
    }
    
    ListNode<Integer> curr = head;
    while(curr!=null && curr.next!=null){
        if(curr.next.value==k){
            ListNode<Integer> kNode = curr.next;
            while(kNode!=null){
                if(kNode.value!=k){
                    break;
                }
                kNode = kNode.next;
            }
            curr.next = kNode;
        }
        curr = curr.next;
    }
    return head;
}
PreviousRemove Duplicates From Sorted ListNextString

Last updated 3 years ago