When the head of the target list we want to return may be different from the original given list, we can use a dummy node linked to the result so that we can get what we want by calling dummy.next.
ListNode dummy = new ListNode(0); ListNode pre = dummy; ListNode cur = head; int dup = Integer.MIN_VALUE;
while(cur != null && cur.next != null) { if(cur.val == cur.next.val) { dup = cur.val; while(cur.val == dup) { cur = cur.next; // if the rest of elements are all the same if(cur == null) { return dummy.next; } } } else { pre.next = cur; pre = pre.next; cur = cur.next; // add null to pre.next: in case the rest of elements are the same and returned above pre.next = null; } } // add the last element (since cur.next != null skip this element) pre.next = cur;
publicclassSolution{ public ListNode reverseBetween(ListNode head, int m, int n){ if(head == null) { return head; }
// find the m position node int count = 1; ListNode dummy = new ListNode(0); dummy.next = head; ListNode pre = dummy; while(count < m) { pre = pre.next; count++; }
// do the reverse: pre-> reverse[first->cur->next] ... ListNode first = pre.next; ListNode cur = pre.next.next; while(count < n) { ListNode next = cur.next; cur.next = pre.next; pre.next = cur; cur = next; count++; if(count == n) { first.next = next; } }
This method will cost O(n) time with no extra space. The space is assigned by system(which could be O(1) or O(h), h is the height of the tree).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public ArrayList<Integer> preorderTraversal(TreeNode root){ ArrayList<Integer> result = new ArrayList<Integer>(); helper(root, result); return result; }
Use a stack to store the nodes. Pay attention to the order of storing. We store the right subtree node before the left one, so that we can get the preorder nodes when pop from the stack. This method will cost O(n) time with O(h) extra space for the stack.
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number does not exist in the array, return -1. Example: If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
int start = 0; int end = nums.length - 1; while(start < end) { int mid = start + (end - start) / 2; if(nums[mid] >= target) { end = mid; } else { start = mid + 1; } }
return nums[start] == target ? start : -1; } }
Key Points:
while(start < end): when start and end are overlapped each other, break. At this moment, we can return start or end. But it is different when we need to return the index before or after the target(see point 3).
int mid = start + (end - start) / 2: in order to avoid overflow of start + end, we need to do a little trick here.
while loop: in the while loop, there are two different needs:
return the index of the target: as the example above.
Here, we need to use while(start <= end) so that when we break the loop, two pointers are at two different(but neighbored) positions. Besides, if we want to return the one before the index, we return start, otherwise we return end.
Tips: A well-known algorithm to solve this problem is using KMP. This method can run in O(m+n) time. But usually, during a 45 minutes interview, complete this O(m*n) method is enough. So, understanding what the point the interviewer asks is very important.
Think it in this way. If I were the interview: This guy may be my potential colleague. Is there any bug here? Because if there are many bugs, I will probably debug a lot for him when we are in a team. Is he talktive? Well, I will give you limited conditions about the problem and I hope you can ask me for more details. Also, to every question, pay attention to the boundry condition.
When we solve a coding problem
Pay attention to the boundry condition
Do communication during the writing part
When finished coding, do a test case no matter the interviewer request or not