Leetcode题目整理【一、链表-中等题-1】

1.两数相加

在这里插入图片描述

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) p = p.next;
            if (q != null) q = q.next;
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return dummyHead.next;
    }
}

2.删除链表的倒数第N个节点

在这里插入图片描述

双指针

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode first = dummy;
        ListNode second = dummy;
        // 因为有dummy节点,第一个指针往前走n+1步
        for (int i = 0; i <= n; i++) {
            first = first.next;
        }
        // 双指针同时向前走
        while (first != null) {
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        return dummy.next;
    }
}

3.两两交换链表中的节点

在这里插入图片描述
迭代

class Solution {
    public ListNode swapPairs(ListNode head) {
        //设置dummy伪节点,用于存pre和返回结果
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        while ((head != null) && (head.next != null)) {
            // 需要交换的节点
            ListNode cur = head;
            ListNode net = head.next;
            // 交换的过程涉及三个节点,pre,cur,net
            pre.next = net;
            cur.next = net.next;
            net.next = cur;
            // Reinitializing the head and prevNode for next swap
            pre = cur;
            head = cur.next; // jump
        }
        // Return the new head node.
        return dummy.next;
    }
}

递归

class Solution {
    public ListNode swapPairs(ListNode head) {
        // 递归结束条件
        if ((head == null) || (head.next == null)) {
            return head;
        }
        // 需要交换的节点
        ListNode firstNode = head;
        ListNode secondNode = head.next;
        // 递归入口
        firstNode.next  = swapPairs(secondNode.next);
        secondNode.next = firstNode;
        // 返回现状的头节点
        return secondNode;
    }
}

4.旋转链表

先确定长度,再确定头节点应该是谁

class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(k==0 || head==null) return head;//特判,防止后面%0
        ListNode pre=new ListNode(0),tmp=head;
        pre.next=head;
        int l=0;
        while(pre.next!=null){
            pre=pre.next;
            l++;
        }
        k%=l;
        if(k==0) return head;//特判,k是l的倍数,不用旋转
        ListNode neil=pre;
        int t=l-k;
        while(--t>0){
            tmp=tmp.next;
        }
        ListNode res=tmp.next;
        neil.next=head;
        tmp.next=null;
        return res;
    }
}

链表中的点已经相连,一次旋转操作意味着:先将链表闭合成环,找到相应的位置断开这个环,确定新的链表头和链表尾

class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        // base cases
        if (head == null) return null;
        else if (head.next == null) return head;

        // close the linked list into the ring
        ListNode old_tail = head;
        int n;
        for(n = 1; old_tail.next != null; n++)
            old_tail = old_tail.next;
        old_tail.next = head;

        // find new tail : (n - k % n - 1)th node
        // and new head : (n - k % n)th node
        ListNode new_tail = head;
        for (int i = 0; i < n - k % n - 1; i++)
            new_tail = new_tail.next;
        ListNode new_head = new_tail.next;

        // break the ring
        new_tail.next = null;

        return new_head;
    }
}

5.分隔链表

双指针

class Solution {
    public ListNode partition(ListNode head, int x) {
        //如果链表只有一个节点或者链表为空则返回
        if(head == null || head.next == null){
            return head;
        }
        //创建一个小于x的链表的哑节点
        ListNode befoe_head = new ListNode(0);
        //创建一个指针指向小于x的链表
        ListNode before_cur = befoe_head;
        ListNode after_head = new ListNode(0);
        //创建一个指针指向大于x的链表
        ListNode after_cur = after_head;
        //循环遍历原链表,当该链表当前节点不为null时
        while(head != null){
            //如果当前节点小于x则把该节点放到before链表上
            if(head.val < x){
                before_cur.next = head;
                before_cur = before_cur.next;
            }else{
                //如果当前节点大于x则把该节点放到after链表上
                after_cur.next = head;
                after_cur = after_cur.next;
            }
            //移动当前节点
            head = head.next;
        }
        after_cur.next = null;
        //将after链表与before链表相连
        before_cur.next = after_head.next;
        return befoe_head.next;
    }
}

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

双指针

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null || head.next==null) {
            return head;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = head;
        ListNode nxt = head.next;
        while(nxt!=null){
            if(cur.val!=nxt.val){
                pre = pre.next;
            }else{
                while(nxt!=null && cur.val == nxt.val){
                    nxt = nxt.next;
                }
                pre.next = nxt;
            }
            cur = pre.next;
            if(cur==null) break;
            nxt = cur.next;
        }
        return dummy.next;
    }
}

递归

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        if (head.val == head.next.val) {
            while (head != null && head.next != null && head.val == head.next.val) {
                head = head.next;
            }
            return deleteDuplicates(head.next);
        } else {
            head.next = deleteDuplicates(head.next);
            return head;
        }
    }
}

7. 重排链表

在这里插入图片描述
找规律之后不难看出,本题分两步:

  1. 翻转后半部分链表
  2. 合并两个链表
class Solution {
    public void reorderList(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) {
            return;
        }
        //找中点,链表分成两个
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode newHead = slow.next;
        slow.next = null;
        //第二个链表倒置
        newHead = reverseList(newHead);
        //链表节点依次连接
        while (newHead != null) {
            ListNode temp = newHead.next;
            newHead.next = head.next;
            head.next = newHead;
            head = newHead.next;
            newHead = temp;
        }
    }
    private ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
}

8. 环形链表 II

在这里插入图片描述

//方法:Floyd算法
public class Solution {
    private ListNode getIntersect(ListNode head) {
        ListNode tortoise = head;
        ListNode hare = head;
        // 双指针找到相遇点
        // 如果hare为null了证明没有环,不然就返回相遇点
        while (hare != null && hare.next != null) {
            tortoise = tortoise.next;
            hare = hare.next.next;
            if (tortoise == hare) {
                return tortoise;
            }
        }
        return null;
    }

    public ListNode detectCycle(ListNode head) {
        if (head == null) {
            return null;
        }
        // 判断getIntersect()方法的结果。
        ListNode intersect = getIntersect(head);
        if (intersect == null) {
            return null;
        }
        // 找到相遇点
        ListNode ptr1 = head;
        ListNode ptr2 = intersect;
        while (ptr1 != ptr2) {
            ptr1 = ptr1.next;
            ptr2 = ptr2.next;
        }
        return ptr1;
    }
}

9. 反转链表 II

在这里插入图片描述
递归。和一般的翻转链表不同,这时的递归首先会判断m是否为1,不为1说明不是翻转的头节点,递归开始后在递归出口处会存储后继节点的值。

class Solution {
    private ListNode successor=null;
    public ListNode reverseBetween(ListNode head, int m, int n) {
       if(m==1) return reverseN(head,n);
       head.next=reverseBetween(head.next,m-1,n-1);
       return head;
    }
    public ListNode reverseN(ListNode head, int n){
        if(n==1){
            successor=head.next;
            return head;
        }
        ListNode last=reverseN(head.next,n-1);
        head.next.next=head;
        head.next=successor;
        return last;
    }
}

10. 复制带随机指针的链表

在这里插入图片描述

public class Solution {
    // 使用HashMap防止创造了之前创造过的点,导致循环递归。
    HashMap<Node, Node> visitedHash = new HashMap<Node, Node>();
    public Node copyRandomList(Node head) {
        //递归结束条件,程序出口
        if (head == null) {
            return null;
        }
        //HashMap中存的是old节点(key)和为它复制的new节点(value)。
        //如果当前的节点已经被复制过了,直接返回即可
        if (this.visitedHash.containsKey(head)) {
            return this.visitedHash.get(head);
        }
        // 执行到这里,说明需要复制,则创建一个新节点
        Node node = new Node(head.val, null, null);
        // 将节点保存在HashMap中,防止递循环归
        this.visitedHash.put(head, node);
        // 递归开始
        node.next = this.copyRandomList(head.next);
        node.random = this.copyRandomList(head.random);
        //返回结果是新创建的node
        return node;
    }
}

11. 对链表进行插入排序

在这里插入图片描述

方法一:将链表排序转化为数组排序
1.创建一个list,遍历链表,将链表中每个节点的值存到list当中;
2.将list排序;
3.从头遍历链表,将每个节点的值依次替换为list中的数据。
class Solution {
    public ListNode insertionSortList(ListNode head) {
        List<Integer> list=new ArrayList<>();
        ListNode p=head;
        while (p!=null){
            list.add(p.val);
            p=p.next;
        }
        Collections.sort(list);
        p=head;
        int i=0;
        while (p!=null){
            p.val=list.get(i++);
            p=p.next;
        }
        return head;
    }
}
方法二:链表插入排序
1.创建一个新的头结点dummy,便于链表的插入(这样原先的头结点head也有前驱节点dummy,不再特殊);
2.遍历链表,如果前后俩个节点已经有序,则不需要操作;
3.否则,从头找到当前节点cur需要插入的位置,插入进去
class Solution {
   public ListNode insertionSortList(ListNode head) {
        if (head==null ||head.next==null)
            return head;
        ListNode pre=head,cur=head.next;           //使用前驱节点pre便于后续节点的删除操作
        ListNode dummy=new ListNode(0);         //建立一个头结点,便于链表的插入
        dummy.next=head;
        while (cur!=null){
            if (pre.val<cur.val){                   //前后节点已经有序,无需重排
                pre=cur;
                cur=cur.next;
            }
            else {
                ListNode p=dummy;
                while (p.next!=cur && p.next.val<cur.val)
                    p=p.next;
                pre.next=cur.next;         //删除当前节点
                cur.next=p.next;          //将当前节点连接到对应位置
                p.next=cur;
                cur=pre.next;
            }
        }
        return dummy.next;
    }
}

12. 排序链表

在这里插入图片描述

方法一:11.中的快排
方法二:并归排序
class Solution {
    public ListNode sortList(ListNode head) {
        return sortList(head, null);
    }
    //排序算法,从head到tail,不包括tail
    public ListNode sortList(ListNode head, ListNode tail) {
        if (head == null) {
            return head;
        }
        //这里需要断开,不然合并的时候会出错
        if (head.next == tail) {
            head.next = null;
            return head;
        }
        //找到进行并归的中点
        ListNode slow = head, fast = head;
        while (fast != tail) {
            slow = slow.next;
            fast = fast.next;
            if (fast != tail) {
                fast = fast.next;
            }
        }
        ListNode mid = slow;
        ListNode list1 = sortList(head, mid);
        ListNode list2 = sortList(mid, tail);
        ListNode sorted = merge(list1, list2);
        return sorted;
    }

    public ListNode merge(ListNode head1, ListNode head2) {
        ListNode dummyHead = new ListNode(0);
        ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
        while (temp1 != null && temp2 != null) {
            if (temp1.val <= temp2.val) {
                temp.next = temp1;
                temp1 = temp1.next;
            } else {
                temp.next = temp2;
                temp2 = temp2.next;
            }
            temp = temp.next;
        }
        if (temp1 != null) {
            temp.next = temp1;
        } else if (temp2 != null) {
            temp.next = temp2;
        }
        return dummyHead.next;
    }
}

13. 奇偶链表

在这里插入图片描述

双指针
class Solution{
    public ListNode oddEvenList(ListNode head){
        // 特判:头结点为 null,返回null
        // head是奇链表的头
        if (head == null) return null;

        // odd是奇链表的当前节点,先初始化为head(初始化为奇链表头)
        ListNode odd  = head;
        // even是偶链表的当前节点,初始化为第二个节点也就是head.next
        ListNode even = head.next;
        // evenHead是偶链表的头节点,初始化为链表第二个节点(初始化为奇链表头的下一个节点)
        ListNode evenHead = even;

        while (even != null && even.next != null){
            // 这里while退出判断条件还是画图一下才能理解(也就是官方题解的STEP2)
            odd.next = even.next;  // 相当于odd.next = odd.next.next(跳过一个偶数节点)
            odd = odd.next;        // odd向前前进一位
            even.next = odd.next;   // 奇链表的下一个节点就是偶链表的节点
            even = even.next;       // even向前前进一位
        }
        // while条件结束,把偶链表头指针拼接到奇链表的最后
        odd.next = evenHead;
        // 返回奇链表头就是返回整个奇偶排序后的链表
        return head;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选和操作应用两大类,单选部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值