Current streak:
1 day
Longest streak:
4 days
Less
More
Node front = new Node(-1);
Node curr = front;
Node rear = null;
public void push(int x) {
// Write Your Code Here
// Here create a dummy node at the first and start adding nodes to the linked list when push operation is called.
//Since queue follows FIFO principle, during pop operation try deleting nodes from the head/front of linked list.
//Now if during midway, the linkedlist becomes empty, create the dummy node again and start attaching nodes again at the end.
if(front==null){
front = new Node(-1);
curr = front;
}
rear = new Node(x);
curr.next = rear;
curr = rear;
}
public int pop() {
// Write Your Code Here
if(front == null || (front.data == -1 && front.next == null)) return -1;
if(front.data==-1){
front = front.next;
}
Node node = front.next;
front.next = null;
int data = front.data;
front = node;
return data;
}
int[] dist = new int[n+1];
for(int i=1;i<=n;i++){
dist[i] = (int) (1e8);
}
dist[src]=0;
// Iterating through (V-1) times
for(int i=1;i<=n-1;i++){
for(List<Integer> edge : edges){
int u = edge.get(0);
int v = edge.get(1);
int wt = edge.get(2);
if(dist[u] + wt < dist[v]){
dist[v] = dist[u] + wt;
}
}
}