This file contains hidden or 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
    
  
  
    
  | public static boolean checkPan(Node a, Node b) { | |
| if(a == null && b == null) return true; | |
| else if (a == null || b == null) return false; | |
| Node ra = reverseR(a); | |
| while(ra != null && b != null) { | |
| if(ra.v != b.v) | |
| return false; | |
| else { | |
| ra = ra.next; | |
| b = b.next; | 
  
    
      This file contains hidden or 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
    
  
  
    
  | public static Node findCircleStart(Node n) { | |
| if(n == null) return n; | |
| HashSet hs = new HashSet<Integer>(); | |
| while(n != null) { | |
| if(hs.add(n.hashCode())) { | |
| n = n.next; | |
| } | |
| else | |
| return n; | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | // reverse add | |
| public static Node add(Node a, Node b) { | |
| if(a == null && b == null) return null; | |
| if(a == null) return b; | |
| if(b == null) return a; | |
| Node sumHead = new Node(-1); | |
| Node sumListPtr = sumHead; | |
| int carry = 0; | |
| while(a != null || b != null) { | |
| int sum = (a == null ? 0: a.v) + (b == null ? 0: b.v) + carry; | 
  
    
      This file contains hidden or 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
    
  
  
    
  | public static Node partition(Node n, int v) { | |
| if(n == null) return null; | |
| Node smallHead = new Node(-1); | |
| Node smallHeadMover = smallHead; | |
| Node bigHead = new Node(-1); | |
| Node bigHeaderMover = bigHead; | |
| while(n != null) { | |
| if(n.v < v) { | |
| if(smallHead.next == null) { | |
| smallHead.next = n; | 
  
    
      This file contains hidden or 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
    
  
  
    
  | public static Node findLastK(Node n, int k) { | |
| if(n == null) return null; | |
| int counter = 0; | |
| Node slow = n; | |
| Node fast = n; | |
| while(fast != null && counter <= k) { | |
| fast = fast.next; | |
| counter++; | |
| } | |
| if(counter < k) return null; // k is longer than the list size | 
  
    
      This file contains hidden or 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
    
  
  
    
  | public static void setZero(int[][] m) { | |
| if(m == null) return; | |
| int d1 = m.length; | |
| int d2 = m[0].length; | |
| // get all the zero element's locations | |
| ArrayList<Zero> z = new ArrayList<Zero>(); | |
| for(int i = 0; i < d1; i++) { | |
| for(int j = 0; j < d2; j++) { | 
  
    
      This file contains hidden or 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
    
  
  
    
  | public static void rotate(int[][] m) { | |
| if(m == null || m.length <= 1) | |
| return; | |
| // rotate by 90: do transponse, then flip the columns. | |
| // transpose: | |
| for(int i = 0; i < m.length; i++) { | |
| for(int j = i; j < m.length; j++) { | |
| int temp = m[i][j]; | |
| m[i][j] = m[j][i]; | |
| m[j][i] = temp; | 
  
    
      This file contains hidden or 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
    
  
  
    
  | public static String compress(String t) { | |
| int i = 0; // fast pointer | |
| int j = 0; // slow pointer | |
| StringBuilder sb = new StringBuilder(); | |
| while(i < t.length()) { | |
| if(t.charAt(j) == t.charAt(i)) i++; | |
| else { | |
| sb.append(t.charAt(j)).append(i-j); | |
| j = i; | |
| } | 
  
    
      This file contains hidden or 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
    
  
  
    
  | // if x.length() < t.length(), pointers start from the string tail | |
| public static String backReplace(String x, String t) { | |
| // null check | |
| if(x == null || t == null) return x; | |
| // convert x into char arrays so as to replace in place | |
| char[] ax = new char[x.length()]; | |
| for(int i = 0; i < x.length(); i++) { | |
| ax[i] = x.charAt(i); | |
| } | |
| int i = x.length() - 1; // front pointer | 
  
    
      This file contains hidden or 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
    
  
  
    
  | // assume all the letters are small | |
| public static boolean checkPermutation(String a, String b) { | |
| if(a == null && b == null) return true; | |
| else if(a == null || b == null) return false; | |
| else if(a.length() != b.length()) return false; | |
| else { | |
| int[] aa = new int[26]; | |
| int[] ab = new int[26]; | |
| for(int i = 0; i < a.length(); i++) { | |
| aa[a.charAt(i) - 'a'] ++; |