在这里插入代码片
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode reverseKGroup (ListNode head, int k) {
// write code here
int count=0;
ListNode cur=head;
while(count!=k&&cur!=null){
cur=cur.next;
count++;
}
if(count==k){
cur=reverseKGroup(cur,k);
while(count!=0){
count--;
ListNode tmp=head.next;
head.next=cur;
cur=head;
head=tmp;
}head=cur;
}return head;
}
}
在这里插入代码片
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
public boolean hasPathSum (TreeNode root, int sum) {
// write code here
if(root==null){
return false;
}
if(root.left==null&&root.right==null&&sum-root.val==0){
return true;
}
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
}
}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode newhead=new ListNode(-1);
ListNode cur=newhead;
if(list1==null){
return list2;
}
if(list2==null){
return list1;
}
while(list1!=null&&list2!=null){
if(list1.val<=list2.val){
cur.next=list1;
cur=list1;
list1=list1.next;
}
else{
cur.next=list2;
cur=list2;
list2=list2.next;
}
}
if(list1!=null){
cur.next=list1;
}
else{cur.next=list2;}
return newhead.next;
}
}