算法-链表问题记录

7 篇文章 0 订阅
  • 链表相加
    class ListNode(var `val`: Int) {
        var next: ListNode? = null
    }
    
    fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
        var list1 = l1
        var list2 = l2
        val result = list1
        var signal = 0

        val sum = list1!!.`val` + list2!!.`val`
        if (sum >= 10) {
            signal = 1
        }
        list1.`val` = sum % 10

        if (1 == signal && null == list1.next && null == list2.next) {
            list2.`val` = 1
            list1.next = list2
            return result
        }

        while (null != list1?.next || null != list2?.next) {
            when {
                null == list1?.next -> {
                    list1?.next = list2?.next
                    if (signal == 1) {
                        list1?.next = list2?.next
                        while (null != list2?.next && signal == 1) {
                            val sum = list2.next!!.`val` + signal
                            signal = if (sum >= 10) 1 else 0
                            list2.next!!.`val` = sum % 10
                            list2 = list2.next
                            if (null == list2?.next && signal == 1) {
                                list2?.next = ListNode(1)
                                return result
                            }
                        }
                    }
                    return result
                }
                null == list2?.next -> {
                    if (signal == 1) {
                        val sum = list1.next!!.`val` + signal
                        signal = if (sum >= 10) 1 else 0
                        list1.next?.`val` = sum % 10
                    } else {
                        return result
                    }
                }
                else -> {
                    val sum = list1.next!!.`val` + list2.next!!.`val` + signal
                    signal = if (sum >= 10) 1 else 0
                    list1.next?.`val` = sum % 10
                    list2 = list2?.next
                }
            }
            list1 = list1.next
            if (1 == signal && null == list1?.next && null == list2?.next) {
                list2?.`val` = 1
                list1?.next = list2
                break
            }
        }
        return result
    }
  • 删除链表的倒数第n节点
  • 数组存储
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode result = head;
        ArrayList<ListNode> list = new ArrayList<>();
        while (head != null) {
            list.add(head);
            head = head.next;
        }
        int length = list.size();
        if (length - n == 0) return result.next;
        if (n == 1) list.get(length - 2).next = null;
        else
            list.get(length - n - 1).next = list.get(length - n + 1);
        return result;
    }
  • 双链表控制长度间隔n
  • 增加一个头节点控制边界
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dump = new ListNode(0);
        ListNode fast = dump;
        ListNode low = dump;
        dump.next = head;
        
        
        int i = 0;
        while(i++<=n){
            fast = fast.next;
        }
        
        while(fast != null){
            low = low.next;
            fast = fast.next;
        }
        
        low.next = low.next.next;
        
        
        return dump.next;
    }
  • 合并两个有序链表
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode temp = head;
        while (l1 != null || l2 != null) {
            if (l2 == null) {
                temp.next = l1;
                temp = temp.next;
                l1 = l1.next;
                continue;
            } else if (l1 == null) {
                temp.next = l2;
                temp = temp.next;
                l2 = l2.next;
                continue;
            }
            if (l1.val < l2.val) {
                temp.next = l1;
                temp = temp.next;
                l1 = l1.next;
            } else {
                temp.next = l2;
                temp = temp.next;
                l2 = l2.next;
            }
        }
        return head.next;
    }
  • 合并k个有序链表
  • 分治法两两合并最快
    public ListNode mergeKLists(ListNode[] lists) {
        final Comparator comparator = new Comparator<Entry<Integer, Integer>>() {
            @Override
            public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
                return o1.value.compareTo(o2.value);
            }
        };

        ListNode r = new ListNode(0);
        ListNode t = r;
        ArrayList<Entry<Integer, Integer>> array = new ArrayList<>();
        ArrayList<ListNode> nodes = new ArrayList<>();
        for (int i = 0; i < lists.length; ++i) {
            if (lists[i] != null)
                array.add(new Entry<>(i, lists[i].val));
            nodes.add(lists[i]);
        }

        array.sort(comparator);

        while (!array.isEmpty()) {
            Entry<Integer, Integer> entry = array.get(0);
            int key = entry.key;
            ListNode temp = nodes.get(key);
            t.next = temp;
            t = t.next;
            if (nodes.get(key).next == null) {
//                nodes.remove(key);
                array.remove(0);
            } else {
                nodes.set(key, nodes.get(key).next);
                array.remove(0);
//                array.add(new Entry<>(key, nodes.get(key).val));
                add(array, new Entry<>(key, nodes.get(key).val));
            }
//            array.sort(comparator);
        }

        return r.next;
    }

    private void add(ArrayList<Entry<Integer, Integer>> array, Entry<Integer, Integer> entry) {
        if (array.size() == 0 || array.get(0).value >= entry.value) {
            array.add(0, entry);
            return;
        }
        for (int i = 0; i < array.size()-1; ++i) {
            if (array.get(i).value <= entry.value && array.get(i+1).value >= entry.value) {
                array.add(i+1, entry);
                return;
            }
        }
        array.add(array.size(), entry);
    }

    class Entry<K, V> {
        Entry(K key, V value) {
            this.key = key;
            this.value = value;
        }

        K key;
        V value;
    }
  • 两两交换链表中的节点
    public ListNode swapPairs(ListNode head) {
        ListNode result = new ListNode(0);
        ListNode r = result;
        while (head!=null) {
            ListNode temp = head;
            if (temp.next == null) {
                r.next = temp;
                r = r.next;
                break;
            }
            head = head.next.next;
            r.next = temp.next;
            r = r.next;
            r.next = temp;
            r = r.next;
        }
        r.next = null;
        return result.next;
    }
  • K 个一组翻转链表
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode result = new ListNode(0);
        ListNode r = result;
        while (head != null) {
            ListNode tHead = head;
            head = connectK(head, k, r);
            r = tHead;
        }
        return result.next;
    }

    private ListNode connectK(ListNode head, int k, ListNode r) {
        ListNode oldHead = head;
        int num = 1;
        ListNode t = head.next;
        head.next = null;
        for (int i = 0; i < k - 1; ++i) {
            if (t == null) {
                r.next = reverse(head);
                return null;
            }
            ++num;
            ListNode tHead = t.next;
            t.next = head;
            head = t;
            t = tHead;
        }
        r.next = head;
        return t;
    }

    private ListNode reverse(ListNode head) {
        ListNode t1 = head;
        ListNode t2 = t1;
        t2 = t2.next;
        t1.next = null;
        while (t2 != null) {
            ListNode t3 = t2.next;
            t2.next = t1;
            t1 = t2;
            t2 = t3;
        }
        return t1;
    }

More info: CSDN

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值