【Leetcode】有关于链表的一些题

1.T160 相交链表

相交链表

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @Description:
 */
public class T160 {
    /**
     * 双指针
     * 最后是空等于空
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pa = headA, pb = headB;
        while(pa != pb){
            pa = pa != null ? pa.next : headB;
            pb = pb != null ? pb.next : headA;
        }
        return pa;
    }
}

2.T206 反转链表

反转链表

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-22 16:56
 * @Description:
 */
public class T206 {

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

3.T21 合并两个有序链表

合并两个有序链表

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-21 21:31
 * @Description:
 */
public class T21 {

    /**
     * 非递归
     * @param l1
     * @param l2
     * @return
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null)
            return l2;
        if(l2 == null)
            return l1;
        if(l1.val <= l2.val){
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
}

4.T83 删除排序链表中的重复元素

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

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-22 17:01
 * @Description:
 */
public class T83 {

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

5.T19 删除链表的倒数第 N 个结点

删除链表的倒数第 N 个结点

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-22 17:14
 * @Description:
 */
public class T19 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        if(fast.next == null){
            //链表已经走到头了
            return head.next;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        //slow的下一个是要删掉的节点
        slow.next = slow.next.next;
        return head;
    }
}

6.T24 两两交换链表中的节点

两两交换链表中的节点

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-23 19:32
 * @Description:
 */
public class T24 {

    public ListNode swapPairs(ListNode head) {
        ListNode res = new ListNode(0);
        res.next = head;
        ListNode front = res, tmp = res.next;
        while (tmp != null && tmp.next != null){
            ListNode next = tmp.next;
            ListNode behind = next.next;
            front.next = next;
            tmp.next = behind;
            next.next = tmp;
            front = tmp;
            tmp = behind;
        }
        return res.next;
    }
}

7.T445 两数相加 II

两数相加 II

import java.util.Stack;

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-23 19:47
 * @Description:
 */
public class T445 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<ListNode> s1 = new Stack<>();
        Stack<ListNode> s2 = new Stack<>();
        ListNode tmp1 = l1, tmp2 = l2;
        while (tmp1 != null){
            s1.push(tmp1);
            tmp1 = tmp1.next;
        }
        while (tmp2 != null){
            s2.push(tmp2);
            tmp2 = tmp2.next;
        }
        ListNode res = new ListNode(-1), p1 = null, p2 = null;
        int digit = 0, now = 0;
        while (!s1.empty() || !s2.empty()){
            p1 = null;
            p2 = null;
            if(!s1.empty())
                p1 = s1.pop();
            if(!s2.empty())
                p2 = s2.pop();
            now = 0;
            if(p1 != null)
                now += p1.val;
            if(p2 != null)
                now += p2.val;
            now += digit;
            digit = now / 10;
            if(res.next == null)
                res.next = new ListNode(now % 10);
            else {
                ListNode next = res.next;
                res.next = new ListNode(now % 10);
                res.next.next = next;
            }
        }
        if(digit != 0){
            ListNode next = res.next;
            res.next = new ListNode(digit);
            res.next.next = next;
        }
        return res.next;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(7);
        l1.next = new ListNode(2);
        l1.next.next = new ListNode(4);
        l1.next.next.next = new ListNode(3);
        ListNode l2 = new ListNode(5);
        l2.next = new ListNode(6);
        l2.next.next = new ListNode(4);
        T445 t445 = new T445();
        ListNode node = t445.addTwoNumbers(l1, l2);
        while (node != null){
            System.out.println(node.val);
            node = node.next;
        }
    }
}

8.T234 回文链表

回文链表

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-24 13:31
 * @Description:
 */
public class T234 {

    public boolean isPalindrome(ListNode head) {
        //找到链表中点
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        //切割链表
        ListNode behind = slow.next;
        slow.next = null;
        //反转后半部分链表
        ListNode pre = null, now = behind, next = null;
        while (now != null){
            next = now.next;
            now.next = pre;
            pre = now;
            now = next;
        }
        //比较两部分是否相等
        ListNode p1 = head, p2 = pre;
        while (p2 != null){
            if(p1.val != p2.val)
                return false;
            p1 = p1.next;
            p2 = p2.next;
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        node.next = new ListNode(2);
        T234 t234 = new T234();
        System.out.println(t234.isPalindrome(node));
    }
}

9.T725 分隔链表

分隔链表

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-24 14:13
 * @Description:
 */
public class T725 {

    public ListNode[] splitListToParts(ListNode root, int k) {
        //统计链表总长度
        ListNode cur = root;
        int len = 0;
        while (cur != null){
            cur = cur.next;
            len ++;
        }
        //计算每段的长度
        int num = len / k;
        int front = len % k;
        //分割链表
        ListNode[] res = new ListNode[k];
        ListNode tmp = root;
        for (int i = 0; i < k && tmp != null; i++) {
            res[i] = tmp;
            int size = num + (front-- > 0 ? 1 : 0);
            for (int j = 0; j < size - 1; j++) {
                tmp = tmp.next;
            }
            ListNode next = tmp.next;
            tmp.next = null;
            tmp = next;
        }
        return res;
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        node.next = new ListNode(2);
        node.next.next = new ListNode(3);
        node.next.next.next = new ListNode(4);
        T725 t725 = new T725();
        ListNode[] nodes = t725.splitListToParts(node, 5);
    }
}

10.T328 奇偶链表

奇偶链表

import java.util.List;

/**
 * @BelongsProject: study
 * @BelongsPackage: PACKAGE_NAME
 * @Author: Alone
 * @CreateTime: 2021-03-07 10:32
 * @Description:
 */
public class T328 {

    public ListNode oddEvenList(ListNode head) {
        if(head == null)
            return null;
        ListNode odd = new ListNode(-1);
        ListNode oddTail = odd;
        ListNode even = new ListNode(-1);
        ListNode evenTail = even;
        boolean flag = true;
        while (head != null){
            if(flag){
                oddTail.next = head;
                oddTail = oddTail.next;
            } else {
                evenTail.next = head;
                evenTail = evenTail.next;
            }
            flag = !flag;
            head = head.next;
        }
        oddTail.next = even.next;
        evenTail.next = null;//不加这行可能会出现循环
        return odd.next;
    }
}

题目来源

https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E9%93%BE%E8%A1%A8.md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值