Last active
March 19, 2024 14:27
-
-
Save yumed15/edb527ea14cf62867341966704a9e227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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