class Solution {
public int[] sortArray(int[] nums) {
Arrays.sort(nums);
return nums;
}
}
第一题摆烂了!
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int pairSum(ListNode head) {
int max=0;
System.out.print(getLength(head));
int length=getLength(head);
int time=length/2;
ListNode reverseList=reverseList(head);
while(time>0){
int temp=head.val+reverseList.val;
if(temp>max){
max=temp;
};
head=head.next;
reverseList=reverseList.next;
time--;
}
return max;
}
//计算链表的长度,反转后相同索引处的元素一个为i,一个为n-1-i
public static int getLength(ListNode head){
if(head==null){
return 0;
}else{
return 1+getLength(head.next);
}
}
public ListNode reverseList(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode pre = head, curr = head.next;
pre.next = null;
while(curr != null){
ListNode next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}
}
反转链表加快慢指针求链表中点。