LeetCode中关于链表的题目整理(待更)

分隔链表(medium难度)

https://leetcode-cn.com/problems/partition-list/

本方法思路及代码来源:

作者:Booooo_
链接:https://leetcode-cn.com/problems/partition-list/solution/fen-ge-lian-biao-lian-biao-jiang-yuan-li-2w5t/
来源:力扣(LeetCode)

解题思路
将链表按条件分成两个链表,然后连接起来即可。
需要为两个链表创建两个值为 0 的虚拟头节点,然后遍历原链表,若当前遍历的节点值大于或等于 x,则 large 指针将该节点 p 与 large 链表连接起来,并 large 指针后移;否则,就是对 small 链表进行操作。

class Solution {

    public ListNode partition(ListNode head, int x) {

        ListNode small = new ListNode(0);
        ListNode smallHead = small;
        ListNode large = new ListNode(0);
        ListNode largeHead = large;

        ListNode p = head;
        while (p != null) {

            if (p.val >= x) {

                large.next = p;
                large = large.next;
            } else {

                small.next = p;
                small = small.next;
            }
            p = p.next;
        }

        small.next = largeHead.next;
        large.next = null;
        return smallHead.next;
    }
}

作者:Booooo_
链接:https://leetcode-cn.com/problems/partition-list/solution/fen-ge-lian-biao-lian-biao-jiang-yuan-li-2w5t/
来源:力扣(LeetCode)

合并两个有序数组(simple难度)

https://leetcode-cn.com/problems/merge-sorted-array/

本题思路算法及代码来源:

作者:guanpengchn
链接:https://leetcode-cn.com/problems/merge-sorted-array/solution/hua-jie-suan-fa-88-he-bing-liang-ge-you-xu-shu-zu-/
来源:力扣(LeetCode)

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int len1 = m - 1;
        int len2 = n - 1;
        int len = m + n - 1;
        while(len1 >= 0 && len2 >= 0) {
            // 注意--符号在后面,表示先进行计算再减1,这种缩写缩短了代码
            nums1[len--] = nums1[len1] > nums2[len2] ? nums1[len1--] : nums2[len2--];
        }
        while (len2 >=0) {
        	nums1[len--] = nums2[len2--];
        }
        // 表示将nums2数组从下标0位置开始,拷贝到nums1数组中,从下标0位置开始,长度为len2+1
        //System.arraycopy(nums2, 0, nums1, 0, len2 + 1);
    }
}

作者:guanpengchn
链接:https://leetcode-cn.com/problems/merge-sorted-array/solution/hua-jie-suan-fa-88-he-bing-liang-ge-you-xu-shu-zu-/
来源:力扣(LeetCode)

奇偶链表(medium难度)

https://leetcode-cn.com/problems/odd-even-linked-list/

11

class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode evenHead = head.next;
        ListNode odd = head, even = evenHead;
        while (even != null && even.next != null) {
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/odd-even-linked-list/solution/qi-ou-lian-biao-by-leetcode-solution/
来源:力扣(LeetCode)

删除排序链表中的重复元素(simple难度)

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/

本方法思路及代码来源:

作者:guanpengchn
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/hua-jie-suan-fa-83-shan-chu-pai-xu-lian-biao-zhong/
来源:力扣(LeetCode)

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

作者:guanpengchn
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/solution/hua-jie-suan-fa-83-shan-chu-pai-xu-lian-biao-zhong/
来源:力扣(LeetCode)

另一种代码:

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        ListNode dummy = new ListNode(-109);
        ListNode tail = dummy;
        while (head != null) {
            // 值不相等才追加,确保了相同的节点只有第一个会被添加到答案
            if (tail.val != head.val) {
                tail.next = head;
                tail = tail.next;
            }
            head = head.next;
        }
        tail.next = null;
        return dummy.next;
    }   
}

作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/
来源:力扣(LeetCode)

删除排序链表中的重复元素Ⅱ(medium难度)

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/

本题方法思路及代码来源:
作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/
来源:力扣(LeetCode)

代码:

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode();
        ListNode tail = dummy;
        while (head != null) {
            // 进入循环时,确保了 head 不会与上一节点相同
            if (head.next == null || head.val != head.next.val) {
                tail.next = head;
                tail = head;
            }
            // 如果 head 与下一节点相同,跳过相同节点
            while (head.next != null && head.val == head.next.val) head = head.next;
            head = head.next;
        }
        tail.next = null;
        return dummy.next;
    }
}

作者:AC_OIer
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/
来源:力扣(LeetCode)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值