刷题7:热题100

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int row = 0;
        int col = matrix[0].length-1;
        while(row<matrix.length && col>=0){
            if (matrix[row][col]==target){
                return true;
            }
            if (matrix[row][col]>target){
                col--;
            }else{
                row++;
            }
        }
        return false;
    }
}

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA = headA;
        ListNode curB = headB;
        int sizeA = 0;
        int sizeB = 0;
        if (curA==null || curB==null) return null;
        while(curA!=null){
            sizeA++;
            curA = curA.next;
        }
        while(curB!=null){
            sizeB++;
            curB = curB.next;
        }
        curA = headA;
        curB = headB;
        if (sizeA>sizeB){
           for (int i=0;i<sizeA-sizeB;i++){
                curA = curA.next;
           }
        }else if (sizeB>sizeA){
            for (int i=0;i<sizeB-sizeA;i++){
                curB = curB.next;
            }
        }
        while(curA!=null){
            if (curA == curB){
                return curA;
            }else{
                curA = curA.next;
                curB = curB.next;
            }
        }
        return null;
    }
}
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        HashMap<ListNode,Integer> map = new HashMap<>();
        ListNode temp = headA;
        while(temp!=null){
            map.put(temp,1);
            temp = temp.next;
        }
        while(headB!=null){
            if (map.containsKey(headB)){
                return headB;
            }else{
                headB = headB.next;
            }
        }
        return null;
    }
}

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while(cur!=null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

class Solution {
    public boolean isPalindrome(ListNode head) {
        int len = 0;
        ListNode cur = head;
        while(cur!=null){
            len++;
            cur = cur.next;
        }
        int[] array = new int[len];
        for (int i=0;i<len;i++){
            array[i] = head.val;
            head = head.next;
        }
        int left = 0;
        int right = len - 1;
        while(left<right){
            if (array[left]!=array[right]) return false;
            left++;
            right--;
        }
        return true;
    }
}
class Solution {
    public boolean isPalindrome(ListNode head) {
        int len = 0;
        ListNode cur = head;
        while(cur!=null){
            len++;
            cur = cur.next;
        }
        int mid = len/2;
        ListNode right = head;
        while(mid>0){
            mid--;
            right = right.next;
        }
        mid = len/2;
        ListNode left = head;
        ListNode pre = null;
        while(mid>0){
            mid--;
            ListNode temp = left.next;
            left.next = pre;
            pre = left;
            left = temp;
        }
        ListNode startleft = pre;
        ListNode startright = null;
        if (len%2==0){
            startright = right;
        }else{
            startright = right.next;
        }
        mid = len/2;
        while(mid>0){
            mid--;
            if (startleft.val!=startright.val) return false;
            startleft = startleft.next;
            startright = startright.next; 
        }
        return true;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值