2022-1-1 82. 删除排序链表中的重复元素 II 86. 分隔链表 92. 反转链表 II 876. 链表的中间结点 217. 存在重复元素 剑指 Offer 03. 数组中重复的数字

82. 删除排序链表中的重复元素 II

/**
 * 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 ListNode deleteDuplicates(ListNode head) {
        //自己写得方法不会处理头结点
        //找到一个重复时利用此值遍历,完成删除,
        ListNode cur=new ListNode(0,head);
        ListNode res=cur;
        while(cur.next!=null&&cur.next.next!=null){
            if(cur.next.val==cur.next.next.val){
                int x=cur.next.val;
                while(cur.next!=null&&cur.next.val==x){
                    cur.next=cur.next.next;
                }
                //[1,1,2,2]
            }else{
                cur=cur.next;
            }
        }
        return res.next;
    }
}

86. 分隔链表

/**
 * 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 ListNode partition(ListNode head, int x) {
        //思路正确,就是用两个链表进行拼接
        //但创建链表时没有想到直接用原链表节点就可以完成拼接
        ListNode small =new ListNode(0);
        ListNode big=new ListNode(0);
        ListNode dummysmall=small;
        ListNode dummybig=big;
        while(head!=null){
            if(head.val<x){
                small.next=head;
                small=small.next;
            }else{
                big.next=head;
                big=big.next;
            }
            head=head.next;
        }
        big.next=null;
        small.next=dummybig.next;
        return dummysmall.next;
    }
}

92. 反转链表 II

/**
 * 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 ListNode reverseBetween(ListNode head, int left, int right) {
       
        //1.没有思路,没有想到头插法
       ListNode p1=new ListNode(0,head);
       ListNode p2=head;
       ListNode p3=p1;
       for(int i=0;i<left-1;i++){
           p1=p1.next;
           p2=p2.next;
       }
       for(int i=0;i<right-left;i++){
           ListNode temp=p2.next;
           p2.next=p2.next.next;
           //头插法总结:
        // 每一个新节点的next始终指向头结点的next;
       //头结点的next始终指向新结点!!!!
           temp.next=p1.next;
           p1.next=temp;
       }   
       //注意如果头结点也发生变化,则返回头节点就不对了,所以利用一个虚拟节点返回     
    //return head;
    return p3.next;
    }
}

876. 链表的中间结点

/**
 * 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 ListNode middleNode(ListNode head) {
        ListNode slow=new ListNode(0,head);
        ListNode fast=new ListNode(0,head);
        while(fast!=null){
            slow=slow.next;
            if(fast.next==null||fast.next.next==null){
                break;
            }
               fast=fast.next.next;
        }
        return slow;
    }
}

217. 存在重复元素

class Solution {
    public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set=new HashSet<>();
        for(int i=0;i<nums.length;i++){
            if(set.contains(nums[i])){
                return true;
            }else{
                set.add(nums[i]);
            }
        }
    return false;
    }
}

剑指 Offer 03. 数组中重复的数字

class Solution {
    public int findRepeatNumber(int[] nums) {
        HashSet<Integer> set=new HashSet<>();
        int res=0;
        for(int i=0;i<nums.length;i++){
            if(set.contains(nums[i])){
                res=nums[i];
                break;
            }else{
                set.add(nums[i]);
            }
        }
        return res;

    }
}

137. 只出现一次的数字 II

class Solution {
    public int singleNumber(int[] nums) {
        Arrays.sort(nums);
        int res=0;
        int flag=0;
        for(int i=0;i<nums.length-3;i=i+3){
            if(nums[i]!=nums[i+1]){
             res=nums[i];
             flag=1;
             break;
            }
        }
        if(flag==0) return nums[nums.length-1];
        return res;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值