/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/classSolution{publicint[]reversePrint(ListNode head){// O(n) & O(1)// 一次遍历:翻转链表 + 记录长度ListNode now = head;ListNode next =null;ListNode temp =null;int len =0;while(now !=null){temp = now.next;now.next = next;next = now;now = temp;len++;}// 二次遍历:赋值int[] ans =newint[len];for(int i =0; i < len; i++, next = next.next){ans[i]= next.val;}return ans;}}
二刷
其实不用翻转也行= =,从后往前填即可
classSolution{publicint[]reversePrint(ListNode head){int len =0;ListNode temp = head;while(temp !=null){len++;temp = temp.next;}int[] ans =newint[len];temp = head;for(int i = len -1; i >=0; i--, temp = temp.next){ans[i]= temp.val;}return ans;}}
文章目录题目描述思路 & 代码二刷题目描述
涉及二进制,位运算跑不了~
思路 & 代码
既然是32位,那么通过一次遍历,每次判断一个位是否为1即可
public class Solution {// you need to treat n as an unsigned valuepu…