Skip to content

Commit

Permalink
Merge pull request #538 from aishwarya-chandra/day9q3
Browse files Browse the repository at this point in the history
day9q3
  • Loading branch information
bh-g authored Jan 24, 2024
2 parents 12bb124 + 2a1f92f commit c80949d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Day 9/q3: Linked List Components/aishwarya--J.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
```
/**
* 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 int numComponents(ListNode head, int[] nums) {
HashSet<Integer> hs = new HashSet<>();
int count = 1;
for(int i = 0; i < nums.length; i++)
hs.add(nums[i]);
boolean bool = true;
boolean bool1 = true;
while(head != null){
if(!hs.contains(head.val) && bool){
count++;
bool = false;
}
else if(hs.contains(head.val))
bool = true;
if(head.next == null && !hs.contains(head.val))
count--;
if(bool1 && !hs.contains(head.val))
count--;
bool1 = false;
head = head.next;
}
return count;
}
}
```

0 comments on commit c80949d

Please sign in to comment.