Skip to content

Instantly share code, notes, and snippets.

@yumed15
Last active March 19, 2024 14:27
Show Gist options
  • Save yumed15/edb527ea14cf62867341966704a9e227 to your computer and use it in GitHub Desktop.
Save yumed15/edb527ea14cf62867341966704a9e227 to your computer and use it in GitHub Desktop.
Map<Integer, DoublyList> freqMap = new HashMap<>();
class DoublyList {
Map<Integer, ListNode> map = new HashMap<>();
ListNode head, tail;
public DoublyList() {
head = new ListNode();
tail = new ListNode();
tail.prev = head;
head.next = tail;
}
public void addNode(ListNode curNode) {
ListNode tailPrev = tail.prev;
tailPrev.next = curNode;
curNode.prev = tailPrev;
tail.prev = curNode;
curNode.next = tail;
map.put(curNode.key, curNode);
}
public ListNode deleteNode(int key) {
if (!map.containsKey(key))
return null;
ListNode curNode = map.get(key);
ListNode prevNode = curNode.prev;
ListNode nextNode = curNode.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
map.remove(key);
return curNode;
}
public ListNode deleteHead() {
if (head.next == tail)
return null;
ListNode firstNode = head.next;
return deleteNode(firstNode.key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment