链表
BM1:反转链表
给定一个单链表的头结点Head(该头节点是有值的),长度为n,反转该链表后,返回新链表的表头。
BM1 题目链接
case1:实际如下所示输入输出
输入:{1,2,3}
输出:{3,2,1}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
import java.io.*;
public class Solution {
public static void ReverseList(ListNode head) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String readline = bufferedReader.readLine();
readline = readline.substring(1,readline.length()-1);
StringBuffer stringBuffer = new StringBuffer();
String[] splits = readline.split(",");
stringBuffer.append("{");
for(int i = splits.length-1 ; i >= 0; i--){
if(i == 0){
stringBuffer.append(splits[i]);
}else{
stringBuffer.append(splits[i]+",");
}
}
stringBuffer.append("}");
System.out.println(stringBuffer.toString());
}
}
case2:链表反转
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) return null;
ListNode pre = null;
ListNode cur = head;
while(cur != null){
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}
二分查找/排序
BM17:二分查找-1
实现无重复数字的升序数组的二分查找
BM17 题目链接
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @param target int整型
* @return int整型
*/
public int search (int[] nums, int target) {
// write code here
int low = 0, high = nums.length-1;
while(low <= high){
int mid = low + (high - low) / 2;
if(nums[mid] == target){
return mid;
}else if(nums[mid] > target){
high = mid - 1;
}else{
low = mid+1;
}
}
return -1;
}
}
二叉树
BM23:二叉树的前序遍历
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
BM23 题目链接