0
点赞
收藏
分享

微信扫一扫

LeeCode刷题简记(五)

皮皮球场 2022-03-13 阅读 45

链表

Solution23.合并K个升序链表、Solution160相交链表、Solution234回文链表、Solution328奇偶链表、Solution237删除链表中的特定元素、Solution24两两交换链表中的节点、Solution61旋转链表、

Solution23.合并K个升序链表

 直接合并再排序

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0){
            return null;
        }
        ArrayList<Integer> nums = new ArrayList<>();
        for (ListNode head : lists) {
            ListNode p = head;
            while (p != null) {
                nums.add(p.val);
                p = p.next;
            }
        }
        Collections.sort(nums);
        ListNode head = new ListNode(-1);
        ListNode p = head;
        for (Integer num : nums) {
            ListNode node = new ListNode(num);
            p.next = node;
            p = p.next;
        }
        return head.next;
    }
}

Solution160相交链表

public class S160相交链表 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null ){
            return null;
        }
        ListNode p1 = headA;
        ListNode p2 = headB;
        while (p1 != p2){
            p1 = p1 == null ? headB : p1.next;//如果p1是空就从b开始走,不是空就下一个
            p2 = p2 == null ? headA : p2.next;
        }
        return p1;
    }
}

Solution234回文链表

快慢指针把链表分成相等两部分,互相遍历找相等,注意fast指向什么表明链表长度奇偶。

public class S234回文链表 {
    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null){
            return true;
        }
        ListNode slow = head,fast = head;
        ListNode p = null,pre = null;
        while (fast != null && fast.next != null){
            p = slow;
            slow = slow.next;
            fast = fast.next.next;
            p.next = pre;
            pre = p;
        }
        if (fast != null){
            slow = slow.next;
            //如果fast不指向空代表是奇数个数,让slow指向头结点的下一条;
        }
        //一一对应判断只要有不一样的就错
        while (p != null && slow != null){
            if (p.val != slow.val){
                return false;
            }
            p = p.next;
            slow = slow.next;
        }
        return true;
    }
}

Solution328奇偶链表

 就是改变链表顺序再把两个连起来。

public class S328奇偶链表 {
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }
        ListNode ji = head;
        ListNode ou = head.next;
        ListNode ouhead = ou;
        while (ou != null && ou.next != null){
            ji.next = ji.next.next;
            ou.next = ou.next.next;
            ji = ji.next;
            ou = ou.next;
        }
        ji.next = ouhead;
        return head;
    }
}

Solution237删除链表中的特定元素

题目给的node就是要删除的节点,链表中删除节点只需要断掉它跟前后的联系,找到人替换自己,再把那个人干掉

class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;    //变成下个倒霉蛋
        node.next = node.next.next;  //把倒霉蛋干掉。
    }
}

Solution24两两交换链表中的节点

迭代法

Solution61旋转链表

class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0 ){
            return head;
        }
        int count = 1;//统计总个数
        ListNode temp = head;
        while (temp.next != null) {
            count++;
            temp = temp.next;
        }
        k %= count;
        //排除k为0时,然后连接首尾,找到k点续到头部
        temp.next = head;
        for (int i = 0 ;i < count - k ; i++) {
            temp = temp.next;
        }
        ListNode newHead = temp.next;
        temp.next = null;
        return newHead;
    }
}

举报

相关推荐

0 条评论