链表

BM1:反转链表

给定一个单链表的头结点Head(该头节点是有值的),长度为n,反转该链表后,返回新链表的表头。
BM1 题目链接
case1:实际如下所示输入输出
输入:{1,2,3}
输出:{3,2,1}

  1. /*
  2. public class ListNode {
  3. int val;
  4. ListNode next = null;
  5. ListNode(int val) {
  6. this.val = val;
  7. }
  8. }*/
  9. import java.io.*;
  10. public class Solution {
  11. public static void ReverseList(ListNode head) throws IOException{
  12. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  13. String readline = bufferedReader.readLine();
  14. readline = readline.substring(1,readline.length()-1);
  15. StringBuffer stringBuffer = new StringBuffer();
  16. String[] splits = readline.split(",");
  17. stringBuffer.append("{");
  18. for(int i = splits.length-1 ; i >= 0; i--){
  19. if(i == 0){
  20. stringBuffer.append(splits[i]);
  21. }else{
  22. stringBuffer.append(splits[i]+",");
  23. }
  24. }
  25. stringBuffer.append("}");
  26. System.out.println(stringBuffer.toString());
  27. }
  28. }

case2:链表反转

  1. /*
  2. public class ListNode {
  3. int val;
  4. ListNode next = null;
  5. ListNode(int val) {
  6. this.val = val;
  7. }
  8. }*/
  9. public class Solution {
  10. public ListNode ReverseList(ListNode head) {
  11. if(head == null) return null;
  12. ListNode pre = null;
  13. ListNode cur = head;
  14. while(cur != null){
  15. ListNode tmp = cur.next;
  16. cur.next = pre;
  17. pre = cur;
  18. cur = tmp;
  19. }
  20. return pre;
  21. }
  22. }

二分查找/排序

BM17:二分查找-1

实现无重复数字的升序数组的二分查找
BM17 题目链接

  1. import java.util.*;
  2. public class Solution {
  3. /**
  4. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
  5. *
  6. *
  7. * @param nums int整型一维数组
  8. * @param target int整型
  9. * @return int整型
  10. */
  11. public int search (int[] nums, int target) {
  12. // write code here
  13. int low = 0, high = nums.length-1;
  14. while(low <= high){
  15. int mid = low + (high - low) / 2;
  16. if(nums[mid] == target){
  17. return mid;
  18. }else if(nums[mid] > target){
  19. high = mid - 1;
  20. }else{
  21. low = mid+1;
  22. }
  23. }
  24. return -1;
  25. }
  26. }

二叉树

BM23:二叉树的前序遍历

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
BM23 题目链接