Skip to content

Commit 010064e

Browse files
single number I/II, remove Nth node from end of list
1 parent c5d5a52 commit 010064e

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package easy;
2+
3+
import utils.CommonUtils;
4+
import classes.ListNode;
5+
6+
/**19. Remove Nth Node From End of List QuestionEditorial Solution My Submissions
7+
Total Accepted: 122330
8+
Total Submissions: 400291
9+
Difficulty: Easy
10+
Given a linked list, remove the nth node from the end of list and return its head.
11+
12+
For example,
13+
14+
Given linked list: 1->2->3->4->5, and n = 2.
15+
16+
After removing the second node from the end, the linked list becomes 1->2->3->5.
17+
Note:
18+
Given n will always be valid.
19+
Try to do this in one pass.*/
20+
public class RemoveNthNodeFromEndOfList {
21+
public ListNode removeNthFromEnd(ListNode head, int n) {
22+
ListNode temp = head;
23+
int len = 0;
24+
while(temp != null){
25+
temp = temp.next;
26+
len++;
27+
}
28+
if(n == len) return head.next;
29+
30+
temp = head;
31+
int cut = len-n;
32+
while(cut-- > 1){
33+
temp = temp.next;
34+
}
35+
if(temp.next != null){
36+
temp.next = temp.next.next;
37+
return head;
38+
}
39+
return null;
40+
}
41+
42+
public static void main(String...strings){
43+
ListNode head = new ListNode(1);
44+
head.next = new ListNode(2);
45+
RemoveNthNodeFromEndOfList test = new RemoveNthNodeFromEndOfList();
46+
ListNode res = test.removeNthFromEnd(head, 2);
47+
CommonUtils.printList(res);
48+
}
49+
}

MEDIUM/src/medium/SingleNumberII.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package medium;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**137. Single Number II QuestionEditorial Solution My Submissions
7+
Total Accepted: 91512
8+
Total Submissions: 236469
9+
Difficulty: Medium
10+
Given an array of integers, every element appears three times except for one. Find that single one.
11+
12+
Note:
13+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
14+
15+
Hide Tags Bit Manipulation
16+
Show Similar Problems
17+
*/
18+
public class SingleNumberII {
19+
20+
public int singleNumber(int[] nums) {
21+
Map<Integer, Integer> map = new HashMap();
22+
for(int i : nums){
23+
map.put(i, map.getOrDefault(i, 0) + 1);
24+
}
25+
for(int key : map.keySet()){
26+
if(map.get(key) != 3) return key;
27+
}
28+
return 0;
29+
}
30+
31+
public static void main(String...strings){
32+
int[] nums = new int[]{2,2,3,2};
33+
SingleNumberII test = new SingleNumberII();
34+
System.out.println(test.singleNumber(nums));
35+
}
36+
37+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package medium;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**260. Single Number III QuestionEditorial Solution My Submissions
7+
Total Accepted: 42536
8+
Total Submissions: 92175
9+
Difficulty: Medium
10+
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
11+
12+
For example:
13+
14+
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
15+
16+
Note:
17+
The order of the result is not important. So in the above example, [5, 3] is also correct.
18+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
19+
*/
20+
public class SingleNumberIII {
21+
//Approach 1: normal hashmap
22+
public int[] singleNumber(int[] nums) {
23+
Map<Integer, Integer> map = new HashMap();
24+
for(int i : nums){
25+
map.put(i, map.getOrDefault(i, 0)+1);
26+
}
27+
28+
int[] res = new int[2];
29+
int index = 0;
30+
for(int key : map.keySet()){
31+
if(map.get(key) == 1) res[index++] = key;
32+
if(index == 2) break;
33+
}
34+
return res;
35+
}
36+
}

0 commit comments

Comments
 (0)