https://github.com/emahtab/remove-linked-list-elements
Remove Linked List Elements
https://github.com/emahtab/remove-linked-list-elements
leetcode linked-list problem-solving
Last synced: about 2 months ago
JSON representation
Remove Linked List Elements
- Host: GitHub
- URL: https://github.com/emahtab/remove-linked-list-elements
- Owner: eMahtab
- Created: 2020-02-09T16:21:17.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-09T16:23:43.000Z (about 6 years ago)
- Last Synced: 2025-08-01T06:37:07.877Z (8 months ago)
- Topics: leetcode, linked-list, problem-solving
- Size: 1000 Bytes
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Remove Linked List Elements
## https://leetcode.com/problems/remove-linked-list-elements
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
## Implementation :
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode current = head;
ListNode prev = null;
while(current != null) {
if(current.val == val){
if(prev != null){
prev.next = current.next;
} else{
head = current.next;
}
} else{
prev = current;
}
current = current.next;
}
return head;
}
}
```