LinkedList Flashcards

1
Q
  1. Design HashMap
    Easy

Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

MyHashMap() initializes the object with an empty map.
void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.

A

class MyHashMap {
int[] a;
public MyHashMap() {
a= new int[1000001];
Arrays.fill(a,-1);
}

public void put(int key, int value) {
     a[key]=value;
}

public int get(int key) {
     return a[key];
}

public void remove(int key) {
      a[key]=-1;
} }

/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q
  1. Reverse Linked List
    Easy

Given the head of a singly linked list, reverse the list, and return the reversed list.

A

/**
* 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 reverseList(ListNode head) {
if (head == null) {
return head;
}
ListNode prev = head;
ListNode cur = head.next;
prev.next = null;
while (cur != null) {
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Merge Two Sorted Lists
    Easy

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

Example 1:

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

A

/**
* 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 mergeTwoLists(ListNode l1, ListNode l2) {

    if (l1 == null) {
        return l2;
    }
    if (l2 == null) {
        return l1;
    }

    ListNode header = new ListNode(-1);
    ListNode curr = header;

    while (l1 != null && l2 != null) {
        int l1val = l1.val;
        int l2val = l2.val;

        if (l1val < l2val) {
            curr.next = l1;
            l1 = l1.next;
        } else {
            curr.next = l2;
            l2 = l2.next;
        }
        curr = curr.next;
    }

    while (l1 != null) {
        curr.next = l1;
        l1 = l1.next;
        curr = curr.next;
    }

    while (l2 != null) {
        curr.next = l2;
        l2 = l2.next;
        curr = curr.next;
    }

    return header.next;
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Intersection of Two Linked Lists
    Easy

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

For example, the following two linked lists begin to intersect at node c1:

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Note that the linked lists must retain their original structure after the function returns.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
listA - The first linked list.
listB - The second linked list.
skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at ‘8’
Explanation: The intersected node’s value is 8 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
- Note that the intersected node’s value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.

A

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> hs = new HashSet<>();
while (headA != null) {
hs.add(headA);
headA = headA.next;
}
while (headB != null) {
if (hs.contains(headB)) {
return headB;
} else {
headB = headB.next;
}
}
return null;
}
}</ListNode>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Palindrome Linked List
    Easy

Given the head of a singly linked list, return true if it is a
palindrome
or false otherwise.

Example 1:

Input: head = [1,2,2,1]
Output: true
Example 2:

Input: head = [1,2]
Output: false

A

/**
* 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 boolean isPalindrome(ListNode head) {
List<Integer> valList = new ArrayList<>();
while(head!=null){
valList.add(head.val);
head = head.next;
}
return checkPalindrome(valList);
}
private boolean checkPalindrome(List valList){
int left = 0;
int right = valList.size()-1;
while(left<right){
if(valList.get(left)==valList.get(right)){
left++;
right--;
}
else return false;
}
return true;
}
}</Integer>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. LRU Cache
    Medium

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

Implement the LRUCache class:

LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
int get(int key) Return the value of the key if the key exists, otherwise return -1.
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
The functions get and put must each run in O(1) average time complexity.

Example 1:

Input
[“LRUCache”, “put”, “put”, “get”, “put”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]

A

class LRUCache {
final Node head= new Node();
final Node tail= new Node();
class Node{
int key;
int val;
Node next;
Node prev;
}

HashMap<Integer, Node> hm;
int capacity;
public LRUCache(int capacity) {
    hm = new HashMap<>();
    this.capacity = capacity;
    head.next = tail;
    tail.prev = head;
}

public int get(int key) {

    if(hm.containsKey(key)){
        Node node = hm.get(key);
        remove(node);
        add(node);
        return node.val;
    }
    return -1;
    
}

public void put(int key, int value) {

    if(hm.containsKey(key)){
        Node node = hm.get(key);
        remove(node);
        node.val = value;
        add(node);
    }
    else{
        if(hm.size()==capacity){
             hm.remove(tail.prev.key);
            remove(tail.prev);
            Node newNode = new Node();
            newNode.key = key;
            newNode.val = value;
            add(newNode);
            hm.put(key,newNode);
        }
        else{
            Node newNode = new Node();
            newNode.key = key;
            newNode.val = value;
            add(newNode);
            hm.put(key,newNode);
        }
    }
    
}

public void add(Node node){
    Node node_next = head.next;
    head.next = node;
    node.prev = head;
    node.next = node_next;
    node_next.prev = node;
    
}

public void remove(Node node){
   Node next_node=node.next;
	Node prev_node=node.prev;
	prev_node.next=next_node;
	next_node.prev=prev_node;
} }

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly