面试:链表序列 I

本博客的目的是为了顺一顺链表中常见的题型 ,备战 2021 年 秋招。
题目的顺序参考 鲂的2021秋招算法总结 其实很多题已经 AC 了 , 总觉得再捋一遍会更踏实 。


链表的结构为
public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
链表中的加法题

1、两数相加
给你两个 非空 的链表,表示两个 非负的整数 。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头 。

示例:
输入:(7 -> 1 -> 6) + (5 -> 9 -> 2),即617 + 295
输出:2 -> 1 -> 9,即912

进阶:思考一下,假设这些数位是正向存放的,又该如何解决呢?

示例:
输入:(6 -> 1 -> 7) + (2 -> 9 -> 5),即617 + 295
输出:9 -> 1 -> 2,即912

2. 两数相加     面试题 02.05. 链表求和

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode newHead = new ListNode(0);
        ListNode cur = newHead;
        int carry = 0;
        while(l1 != null || l2 != null){
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + carry;

            carry = sum / 10;
            sum %= 10;
            cur.next = new ListNode(sum);

            cur = cur.next;
            if(l1 != null)
                l1 = l1.next;
            if(l2 != null)
                l2 = l2.next;
        }
        if(carry == 1){
            cur.next = new ListNode(carry);
        }
        return newHead.next;
    }
}

进阶 :445. 两数相加

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while (l1 != null){
            stack1.push(l1.val);
            l1 = l1.next;
        }
        while (l2 != null){
            stack2.push(l2.val);
            l2 = l2.next;
        }
        int pre = 0;
        ListNode newHead = new ListNode(-1);
        while (!stack1.isEmpty() || !stack2.isEmpty() || pre != 0) {
            int a = stack1.isEmpty() ? 0 : stack1.pop();
            int b = stack2.isEmpty() ? 0 : stack2.pop();
            int sum = a + b + pre;
            pre = sum / 10; 
            ListNode cur = new ListNode(sum - pre * 10);
            cur.next = newHead.next;
            newHead.next = cur;
        }
        return newHead.next;
    }
}
反转链表的题

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

示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

206. 反转链表     剑指 Offer 24. 反转链表

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode newHead = new ListNode(0);
        while(head != null){
            ListNode temp = head;
            head = head.next;
            temp.next = newHead.next;
            newHead.next = temp;
        }
        return newHead.next;
    }
}

进阶版 : 区间内反转 92. 反转链表 II , 此题比较巧

class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for (int i = 0; i < m - 1; i++) {
            pre = pre.next;
        }
        ListNode cur = pre.next;
        ListNode next;
        for (int i = 0; i < n - m; i++) {
            next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return dummyNode.next;
    }
}

2、重排链表
给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
143. 重排链表

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;
        }
    }

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode tail = head;
        head = head.next;

        tail.next = null;

        while (head != null) {
            ListNode temp = head.next;
            head.next = tail;
            tail = head;
            head = temp;
        }
        return tail;
    }
}

3、K 个一组翻转链表
给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
k 是一个正整数,它的值小于或等于链表的长度。
如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
进阶:
你可以设计一个只使用常数额外空间的算法来解决此问题吗?
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
25. K 个一组翻转链表
题目分析:
和上面的 区间内反转 思路一样

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        int count = 0 ;
        ListNode newHead = new ListNode(-1); 
        newHead.next = head;
        for(ListNode p1 = head ; p1 != null ; p1 = p1.next){
            count ++;
        }
        int cnt = count / k;

        ListNode pre = newHead;
        ListNode cur = pre.next;
        ListNode next;
        for(int i = 0 ; i < cnt ; i ++){
            for(int j = i * k ; j < (i + 1) * k - 1 ; j ++){
                next = cur.next;
                cur.next = next.next;
                next.next = pre.next;
                pre.next = next;
            }
            pre = cur;
            cur = pre.next;
        }
        return newHead.next;
    }
}

4、回文链表
请判断一个链表是否为回文链表。
题目分析 : 先通过 快慢指针 找到链表的中间节点(偶数长度时 :中间右边的) , 然后把链表后部分翻转 , 前后部分进行比较 。
234. 回文链表     面试题 02.06. 回文链表

/**
    head        1 
                    2
                        3  --> null
                    2
    subHead     1

    head        1
                    2
                        2 --> null
    subHead         1
 */
class Solution {
    //本质上是,将从中间节点之后节点反转,把中间节点作为两个头指针所指向链表的终点
    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode mid = generateMid(head);
        ListNode subHead = reverse(mid);
        //遍历
        while (subHead != null) {
            if (subHead.val != head.val) {
                return false;
            }
            subHead = subHead.next;
            head = head.next;
        }
        return true;
    }
    //反转链表
    private ListNode reverse(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
    //奇数寻找中间节点,或者偶数右中位数节点
    private ListNode generateMid(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值