链表相关题目总结(含leetcode链表题Java代码)

这篇博客总结了常见的链表操作题目,包括删除倒数第n个节点、两个有序链表的合并、环的检测、链表倒置、找到链表中间节点等,并详细介绍了LeetCode中的一系列链表相关题目,如两数相加、两两交换节点、删除重复元素、分隔链表、倒置链表等。同时分享了解答链表题目的技巧,如使用哨兵和分链解题方法,以及需要注意的测试用例。
摘要由CSDN通过智能技术生成

链表是最常见的数据结构之一,下面主要总结题目,不涉及基本的数据结构的介绍

1. 常见的链表操作题目

1.1 删除链表倒数第 n 个结点(leetcode 19)

只遍历一次,保证输入的 n 是有效的,问如何删除倒数第 n 个结点

    // 思路:一快一慢两个节点进行遍历,快的结点比慢的结点先走 n 步
    public ListNode removeNthFromEnd(ListNode head, int n) {
   
        // 设置哨兵结点,处理涉及头结点的情况
        ListNode start = new ListNode(0);
        ListNode slow = start, fast = start;
        slow.next = head;
        // 快结点提前走 n + 1 步(包含了头结点)
        for (int i = 0; i <= n; i++)   {
   
            fast = fast.next;
        }
        // 找出倒数第 n 个结点
        while (fast != null) {
   
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return start.next;
    }

1.2 两个有序链表的合并(leetcode 21)

确定初始节点,之后两条链表一边遍历一边比较跟赋值,直到一条链遍历完,将另一条链剩余的部分接在输出的链尾上

	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
   
		// 快速返回,如果有一边为空,则合并结果为另外一个队列
        if (l1 == null) {
   
            return l2;
        }
        if (l2 == null) {
   
            return l1;
        }
        
        ListNode head = new ListNode(0);
        ListNode temp = head;
        // 遍历,直到有一个队列被遍历完
        while (l1 != null && l2 != null) {
   
            if(l1.val < l2.val) {
   
                temp.next = l1;
                l1 = l1.next;
            } else {
   
                temp.next = l2;
                l2 = l2.next;
            }
            temp = temp.next;
            temp.next = null;
        }
        // 将另外一个未被遍历完的队列,接在合并队列的后面
        if (l1 == null) {
   
            temp.next = l2;
        }
        if (l2 == null) {
   
            temp.next = l1;
        }

        return head.next;
    }

1.3 链表中环的检测(leetcode 141)

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?

    // 用一快一慢两个结点进行遍历,快结点一次遍历两个结点,慢结点一次遍历一个结点
    // 如果有环存在的话,快结点无法遍历到链表的末尾,最终会与慢结点相遇
    public boolean hasCycle(ListNode head) {
   
        if (head == null) {
   
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
   
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
   
                return true;
            }
        }
        return false;
    }

1.4 链表倒置(leetcode 206)

思路:先考虑如何改变链表中部的指向问题,之后考虑开头跟结尾的特殊情况

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

1.5 链表的中间结点(leetcode 876)

Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.

    public ListNode middleNode(ListNode head) {
   
        ListNode a = head;
        ListNode b = head;
        while (a != null && a.next != null) {
   
            a = a.next.next;
            b = b.next;
        }
        return b;
    }

2. leetcode 其他相关题目

代码 github 链接:https://github.com/Parallelline1996/-Algorithm/tree/master/Leetcode/src

No.2 Add Two Numbers(两数相加)

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.

  • Example:
    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.
	/*
	思路:注意这里不能将两个数读出来再相加,因为涉及一个Integer类型,Long类型有最大值的问题
	注意这里链表是将数字进行倒置,所以刚好两个链表从头到尾遍历,就是两个数从末一位
	分别相加到最高位的过程,这里要注意相加过程中的进位问题。
	*/
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
   
        ListNode output = null;
        ListNode head = null;
        int a, b, add = 0, total;
        while (l1 != null || l2 != null) {
   
            if (l1 != null) {
   
                a = l1.va
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值