☆25 K个一组翻转链表(找到待翻转链表的前面一个节点,后面一个节点,第一个节点,最后一个节点+链表尾指针置空链表翻转再连接;当待翻转链表长度小于k时直接返回)
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode newhead=new ListNode(-1);
newhead.next=head;
ListNode pre=newhead;
ListNode end=pre;
while(end.next!=null){
for(int i=0;i<k&&end!=null;i++) end=end.next;
if(end==null) break;
ListNode next=end.next;
end.next=null;
ListNode start=pre.next;
pre.next=reverse(start);
start.next=next;
pre=start;
end=pre;
}
return newhead.next;
}
public ListNode reverse(ListNode head){
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
ListNode r=cur.next;
cur.next=pre;
pre=cur;
cur=r;
}
return pre;
}
}
103二叉树的锯齿形层次遍历
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ret=new ArrayList<>();
if(root==null) return ret;
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(root);
int levelsize;
int sign=0;
while(!queue.isEmpty()){
levelsize=queue.size();
List<Integer> list=new ArrayList<>();
if(++sign%2==0){
Deque<Integer> stack=new LinkedList<>();
while(levelsize-->0){
TreeNode node=queue.poll();
stack.push(node.val);
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
while(!stack.isEmpty()){
list.add(stack.pop());
}
}else{
while(levelsize-->0){
TreeNode node=queue.poll();
list.add(node.val);
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
}
ret.add(list);
}
return ret;
}
}
☆54 螺旋矩阵(记录上下边界是否越界,越界返回;不越界顺时针四周遍历)
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> ret=new ArrayList<>();
if(matrix==null||matrix.length==0||matrix[0].length==0) return ret;
int up=0,down=matrix.length-1,left=0,right=matrix[0].length-1;
while(true){
for(int col=left;col<=right;col++){
ret.add(matrix[up][col]);
}
if(++up>down) break;
for(int row=up;row<=down;row++){
ret.add(matrix[row][right]);
}
if(--right<left) break;
for(int col=right;col>=left;col--){
ret.add(matrix[down][col]);
}
if(--down<up) break;
for(int row=down;row>=up;row--){
ret.add(matrix[row][left]);
}
if(++left>right) break;
}
return ret;
}
}
415 字符串相加(类似两个链表节点相加,从后往前,结果逆序需要翻转)
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder sb=new StringBuilder("");
int i=num1.length()-1,j=num2.length()-1;
int tmp=0;
while(i>=0||j>=0){
int val1=i<0? 0:num1.charAt(i)-'0';
int val2=j<0? 0:num2.charAt(j)-'0';
int sum=val1+val2+tmp;
tmp=sum/10;
sum=sum%10;
sb.append(sum);
i=i<0? i:i-1;
j=j<0? j:j-1;
}
if(tmp!=0){
sb.append(tmp);
}
return sb.reverse().toString();
}
}
199 二叉树的右视图(层序遍历,每层只保存最右边的节点)
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ret=new ArrayList<>();
if(root==null) return ret;
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int levelsize=queue.size();
while(levelsize-->0){
TreeNode node=queue.poll();
if(levelsize==0) ret.add(node.val);
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
}
return ret;
}
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
88 合并两个有序数组(原地合并,从后往前合并,维护三个指针)
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i=nums1.length-1;
m--;
n--;
while(n>=0){
if(m>=0&&nums1[m]>nums2[n]){
nums1[i--]=nums1[m--];
}else{
nums1[i--]=nums2[n--];
}
}
}
}
143 重排链表(找到中间节点+翻转右半边链表+依次连接两个链表;使用ArrayList存储节点+双指针重连链表)
class Solution {
public void reorderList(ListNode head) {
if(head==null||head.next==null) return;
ListNode slow=head,fast=head.next;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
ListNode newhead=slow.next;
slow.next=null;
newhead=reverseList(newhead);
ListNode p1=head,p2=newhead;
while(p1!=null&&p2!=null){
ListNode r1=p1.next;
ListNode r2=p2.next;
p1.next=p2;
p2.next=r1;
p1=r1;
p2=r2;
}
}
public ListNode reverseList(ListNode head){
if(head==null||head.next==null) return head;
ListNode newhead=reverseList(head.next);
head.next.next=head;
head.next=null;
return newhead;
}
}
class Solution {
public void reorderList(ListNode head) {
if(head==null||head.next==null) return;
List<ListNode> list=new ArrayList<>();
ListNode cur=head;
while(cur!=null){
list.add(cur);
cur=cur.next;
}
int i=0,j=list.size()-1;
for(;i<j;i++,j--){
ListNode r1=list.get(i).next;
list.get(i).next=list.get(j);
list.get(j).next=r1;
}
list.get(i).next=null;
}
}