奇数放偶数前面
首尾双指针
左指针往右移 右指针往左移
class Solution {
public int[] exchange(int[] nums) {
int i=0;int j=nums.length-1;
while(i<j){
//注意一定要有i<j这个判定条件
while(i<j&&nums[i]%2==1){
++i;
}
while(i<j&&nums[j]%2==0) {
--j;
}
int x=nums[i];
nums[i]=nums[j];
nums[j]=x;
}
return nums;
}
}
合并两个排序的链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
if(l2 == null) return l1;
ListNode m=new ListNode(0);
ListNode m1=m;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
m1.next=new ListNode(l1.val);
m1=m1.next;
l1=l1.next;
}
else{
m1.next=new ListNode(l2.val);
m1=m1.next;
l2=l2.next;
}
}
while(l1!=null){
m1.next=new ListNode(l1.val);
m1=m1.next;
l1=l1.next;
}
while(l2!=null){
m1.next=new ListNode(l2.val);
m1=m1.next;
l2=l2.next;
}
return m.next;
}
}