203. Remove Linked List Elements
https://leetcode.com/problems/remove-linked-list-elements/
Intuition
Have a prev pointer
If head.val == target
Set the prev to head.next
else
prev = prev.next
Preprocess
Corner case, while head.val == target at the beginning of list
--
Related problems
237-delete-node-in-linked-list
---
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5--
Intuition
Have a prev pointer
If head.val == target
Set the prev to head.next
else
prev = prev.next
Preprocess
Corner case, while head.val == target at the beginning of list
--
Related problems
237-delete-node-in-linked-list
---
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode() {} | |
* ListNode(int val) { this.val = val; } | |
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | |
* } | |
*/ | |
class Solution { | |
public ListNode removeElements(ListNode head, int val) { | |
while (head != null && head.val == val) { | |
head = head.next; | |
} | |
ListNode ans = head; | |
while (head != null && head.next != null) { | |
if (head.next.val == val) { | |
head.next = head.next.next; | |
} else { | |
head = head.next; | |
} | |
} | |
return ans; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode() {} | |
* ListNode(int val) { this.val = val; } | |
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | |
* } | |
*/ | |
class Solution { | |
public ListNode removeElements(ListNode head, int val) { | |
ListNode prev = new ListNode(-1), current = head; | |
prev.next = head; | |
ListNode ans = prev; | |
while (current != null) { | |
if (current.val == val) { | |
prev.next = current.next; | |
} else { | |
prev = prev.next; | |
} | |
current = current.next; | |
} | |
return ans.next; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, val=0, next=None): | |
# self.val = val | |
# self.next = next | |
class Solution: | |
def removeElements(self, head: ListNode, val: int) -> ListNode: | |
prev = ListNode(-1) | |
prev.next = head | |
current = head | |
ans = prev | |
while current: | |
if current.val == val: | |
prev.next = current.next | |
else: | |
prev = prev.next | |
current = current.next | |
return ans.next |