@jianchao.li.fighter A really neat idea! However I think it might be slightly difficult to grasp.
nrl
@nrl
Posts made by nrl

RE: Easy Binary Search in Java  O(log(n)) time

RE: Incorrect input?
@lkjhgfdsa Yeah, I think it was poorly worded like the 2 problems after it. I tried out both (max and total) to see which one worked.

Easy BFS in Java
We need to modify BFS, since visiting a same node with different travelled distance can lead to lower distances. So instead of checking if a node has been visited before adding it to the queue, we check if we can lower the distance on that node.
class Solution { public int networkDelayTime(int[][] times, int N, int K) { int[][] graph = new int[N + 1][N + 1]; //Keep 1 as invalid edge since there were test cases with edge weight of 0 for (int[] row : graph) Arrays.fill(row, 1); for (int[] time : times) { int u = time[0], v = time[1], w = time[2]; graph[u][v] = w; } int[] dist = new int[N + 1]; Arrays.fill(dist, Integer.MAX_VALUE); Deque<Integer> nodeQ = new ArrayDeque<>(); Deque<Integer> distQ = new ArrayDeque<>(); nodeQ.offer(K); distQ.offer(0); while (!nodeQ.isEmpty()) { int u = nodeQ.poll(); int d = distQ.poll(); dist[u] = Math.min(d, dist[u]); for (int v = 1; v <= N; v++) { //Check if edge exists between u and v and if we can improve the distance to v from K if (graph[u][v] != 1 && dist[v] > graph[u][v] + d) { nodeQ.offer(v); distQ.offer(d + graph[u][v]); } } } int res = 0; for (int i = 1; i <= N; i++) { //INT_MAX implies no path was found between K and i if (dist[i] == Integer.MAX_VALUE) return 1; else res = Math.max(res, dist[i]); } return res; } }

RE: Incorrect input?
@lkjhgfdsa It takes 2 time units for the information to flow from 2 to 4, and it takes 1 time unit for the information to flow from 2 to 1 and 2 to 3. So after 1 time unit information is passed to nodes 1 and 3 and after 2 time units the information has been passed to node 4. Thus after 2 time units information is successfully passed to all the nodes in the graph and hence 2 is the answer.

Easy Binary Search in Java  O(log(n)) time
Binary search for the character which comes immediately after character target in the alphabets, or if the target is greater than or equal to the last character in the input list, then search for the first character in the list.
class Solution { public char nextGreatestLetter(char[] a, char x) { int n = a.length; if (x >= a[n  1]) x = a[0]; else x++; int lo = 0, hi = n  1; while (lo < hi) { int mid = lo + (hi  lo) / 2; if (a[mid] == x) return a[mid]; if (a[mid] < x) lo = mid + 1; else hi = mid; } return a[hi]; } }

Single pass in Java with comments
Add the new interval just before another interval which starts after the new interval, while checking that there is no overlap with the another interval just before. Be careful to add new interval at the end, if we cannot find any interval which starts after the new interval. Since we keep the intervals in sorted order, we only need to look through the intervals linearly to check overlap.
Time: O(n) Space: O(1) //We don't use any auxiliary space class MyCalendar { class Interval { int start, end; Interval (int s, int e) { start = s; end = e; } } List<Interval> itv; public MyCalendar() { itv = new ArrayList<>(); } public boolean book(int start, int end) { for (int i = 0; i < itv.size(); i++) { if (itv.get(i).start >= end) { if (i == 0  (itv.get(i  1).end <= start)) { //make sure there is no overlap with the previous interval itv.add(i, new Interval(start, end)); return true; } } } //if this interval starts after all the other previous intervals if (itv.size() == 0  itv.get(itv.size()  1).end <= start) { itv.add(new Interval(start, end)); return true; } return false; } }

RE: One pass solution in Java
Thanks for the kind words @ihaveayaya. I have made changes accordingly and also added some comments.

Short and Easy solution in Java
class Solution { public String longestWord(String[] words) { Set<String> hs = new HashSet<String>(Arrays.asList(words)); String maxWord = ""; for (String w : words) { int i; for (i = 1; i <= w.length(); i++) { if (!hs.contains(w.substring(0, i))) break; } if (i > w.length()) { if (w.length() > maxWord.length()  (w.length() == maxWord.length() && w.compareTo(maxWord) < 0)) { maxWord = w; } } } return maxWord; } }

One pass solution in Java
We only need to check for two things:
 If we see '//' we stop reading the current line, and add whatever characters we have seen to the result.
 If we see '/*' then we start the multiline comment mode and we keep on ignoring characters until we see '*/'.
 If the current character is neither of the above two and the multiline comment mode is off, then we add that character to the current line.
Once we parse one line (source[i]), then if the mode is off, we add the currently generated line (StringBuilder) to the result and repeat for source[i + 1].
We need to be careful not to insert empty lines in the result.
class Solution { public List<String> removeComments(String[] source) { List<String> res = new ArrayList<>(); StringBuilder sb = new StringBuilder(); boolean mode = false; for (String s : source) { for (int i = 0; i < s.length(); i++) { if (mode) { if (s.charAt(i) == '*' && i < s.length()  1 && s.charAt(i + 1) == '/') { mode = false; i++; //skip '/' on next iteration of i } } else { if (s.charAt(i) == '/' && i < s.length()  1 && s.charAt(i + 1) == '/') { break; //ignore remaining characters on line s } else if (s.charAt(i) == '/' && i < s.length()  1 && s.charAt(i + 1) == '*') { mode = true; i++; //skip '*' on next iteration of i } else sb.append(s.charAt(i)); //not a comment } } if (!mode && sb.length() > 0) { res.add(sb.toString()); sb = new StringBuilder(); //reset for next line of source code } } return res; } }
Thanks to @ihaveayaya for suggestion to remove some duplicate code.

RE: Reverse Linked List
@HanYuxin, it should be while (cur != null) instead of if (cur != null)