[5.22刷题-算法]链表专题

[5.22刷题-算法]链表专题

一、[Leetcode:2] 两数相加

(https://leetcode-cn.com/problems/add-two-numbers/)

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

在这里插入图片描述

解法:

采用一个额外的链表进行一个存储,设置一个flag值来记录需不需要进位的一个操作,最后输出新建立的链表即可,在空间上由于新建立了一个链表,所以消耗略多
在这里插入图片描述

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode pre=new ListNode(0);
        ListNode head=pre;
        int flag=0;
        while(l1!=null||l2!=null){
            int res=0;
            if(l1!=null&&l2!=null) res=l1.val+l2.val+flag;
            else{
                res=l1==null?l2.val+flag:l1.val+flag;
            }
            flag=res/10;
            res=res%10;
            head.next=new ListNode(res);
            head=head.next;
            l1=l1==null?l1:l1.next;
            l2=l2==null?l2:l2.next;
        }
        if(flag==1) head.next=new ListNode(1);
        return pre.next;
    }

二、[Leetcode:19]删除链表的倒数第N个结点

(https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/)

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

**进阶:**你能尝试使用一趟扫描实现吗?

在这里插入图片描述

解法一:(双指针)

在这里插入图片描述

  • 首先快指针线往前走n个
  • 之后慢的指针从头开始和快指针一起往前走,直到最后
  • 之后删除后返回链表即可
public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast=head;
        while(n!=0&&fast!=null){
            fast=fast.next;
            n--;
        }
        if(fast==null) return head.next==null?null:head.next;
        ListNode slow=head;
        while(fast.next!=null){
            slow=slow.next;
            fast=fast.next;
        }
        slow.next=slow.next.next;
        return head;
    }
解法二(看题解得出的:栈)
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        Deque<ListNode> stack = new LinkedList<ListNode>();
        ListNode cur = dummy;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        for (int i = 0; i < n; ++i) {
            stack.pop();
        }
        ListNode prev = stack.peek();
        prev.next = prev.next.next;
        ListNode ans = dummy.next;
        return ans;
    }
}

三、[Leetcode:25]k组一个翻转链表

(https://leetcode-cn.com/problems/reverse-nodes-in-k-group/)

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。

k 是一个正整数,它的值小于或等于链表的长度。

如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

进阶:

你可以设计一个只使用常数额外空间的算法来解决此问题吗?
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

在这里插入图片描述

解法:分部翻转,然后和前面有一个翻转链表的结合起来

在这里插入图片描述

public ListNode reverseKGroup(ListNode head, int k) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode pre = dummy;
    ListNode end = dummy;
    while (end.next != null) {
        for (int i = 0; i < k && end != null; i++) end = end.next;
        if (end == null) break;
        ListNode start = pre.next;
        ListNode next = end.next;
        end.next = null;
        pre.next = reverse(start);
        start.next = next;
        pre = start;
        end = pre;
    }
    return dummy.next;
}

private ListNode reverse(ListNode head) {
    ListNode pre = null;
    ListNode curr = head;
    while (curr != null) {
        ListNode next = curr.next;
        curr.next = pre;
        pre = curr;
        curr = next;
    }
    return pre;
}

四、[Leetcode:61]旋转链表

(https://leetcode-cn.com/problems/rotate-list/)

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

题解:环!!

在这里插入图片描述

 public ListNode rotateRight(ListNode head, int k) {
        if(head == null || k == 0)
            return head;
        ListNode l1 = head;
        int len = 1;
        while(l1.next != null) {
            l1 = l1.next;
            len++;
        }
        l1.next = head;
        ListNode l2 = head;
        for(int i = 1; i < len - k % len; i++) 
            l2 = l2.next;
        ListNode newHead = l2.next;
        l2.next = null;
        return newHead;
    }

五、[leetcode:206]链表旋转

(https://leetcode-cn.com/problems/reverse-linked-list/)

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

在这里插入图片描述

题解一(迭代)

在这里插入图片描述

public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
题解二(递归)
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}
 {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值