/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/classSolution{publicListNodegetKthFromEnd(ListNode head,int k){ListNode slow=head,fast=head;while(k>0){
fast=fast.next;
k--;}while(fast!=null){
fast=fast.next;
slow=slow.next;}return slow;}}
2.反转链表->pre,cur,next
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/classSolution{publicListNodereverseList(ListNode head){ListNode pre=null,cur=head,next=null;while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;}return pre;}}
3.合并两个排序的链表->逐个节点比较
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/classSolution{publicListNodemergeTwoLists(ListNode l1,ListNode l2){if(l1==null)return l2;if(l2==null)return l1;ListNode dummyHead=newListNode(0);//创建一个假头,就可以省略头部的比较ListNode cur=dummyHead;while(l1!=null&& l2!=null){if(l1.val<l2.val){
cur.next=l1;
cur=cur.next;
l1=l1.next;}else{
cur.next=l2;
cur=cur.next;
l2=l2.next;}}if(l1!=null){
cur.next=l1;}if(l2!=null){
cur.next=l2;}return dummyHead.next;}}
4.树的子结构->递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/classSolution{publicbooleanisSubStructure(TreeNodeA,TreeNodeB){if(A==null||B==null){returnfalse;}//如果A和B不一致,再去遍历A的左右子树,看和B是否有一致的returndfs(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B);}//遍历树A和B,看是否一致publicbooleandfs(TreeNodeA,TreeNodeB){if(B==null)returntrue;//B可以为空,这时候可以是B已经遍历完了if(A==null)returnfalse;//但是在B不为空情况下,A不能为空,如果A为空,就不是子树return(A.val==B.val)&&dfs(A.left,B.left)&&dfs(A.right,B.right);}}
5.二叉树的镜像->递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/classSolution{publicTreeNodemirrorTree(TreeNode root){//交换左右节点//特殊情况if(root==null||(root.left==null&& root.right==null)){return root;}//应该自顶向下交换//(1)先交换当前节点的左右子节点TreeNode temp=null;
temp=root.left;
root.left=root.right;
root.right=temp;//(2)再向下递归mirrorTree(root.left);mirrorTree(root.right);return root;}}
6.对称的二叉树->递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/classSolution{publicbooleanisSymmetric(TreeNode root){if(root==null){returntrue;}returnhelper(root.left,root.right);}publicbooleanhelper(TreeNode node1,TreeNode node2){if(node1==null&& node2==null){returntrue;}if(node1==null|| node2==null){returnfalse;}return(node1.val==node2.val)&&helper(node1.left,node2.right)&&helper(node1.right,node2.left);}}
classMinStack{privateDeque stackA;//数据栈privateDeque stackB;//辅助栈,始终将最小值存放在栈顶/** initialize your data structure here. */publicMinStack(){
stackA=newLinkedList();
stackB=newLinkedList();}publicvoidpush(int x){
stackA.push(x);//当栈B为空或者栈顶元素大于x时,将x放在B的栈顶if(stackB.isEmpty()||(int)stackB.peek()>=x){
stackB.push(x);}}publicvoidpop(){//如果Apop出去的元素和B栈顶元素相等,那么就是要把最小元素pop出去,所以B也要弹栈if((int)stackA.pop()==(int)stackB.peek()){
stackB.pop();}}publicinttop(){return(int)stackA.peek();}publicintmin(){return(int)stackB.peek();}}/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/