美文网首页
三道反转链表

三道反转链表

作者: Sczlog | 来源:发表于2019-08-07 15:07 被阅读0次

206.Reverse Linked List

Reverse a singly linked list.

简单题目不简单,数据结构的基础,在不生成新链表的情况下原地生成一个反转链表。

var reverseList = (head) => {
    let tail = head;
    if (!head || !head.next) {
        return head;
    }
    while (tail.next) {
        let tmp = tail.next.next;
        tail.next.next = head;
        head = tail.next;
        tail.next = tmp;
    }
    return head;
};

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

上一题的扩展,只反转一部分链表,可以继续沿用上题的部分思想,通过一个depth来确认链表反传到了哪一层来跳出循环。

var reverseBetween = function (head, m, n) {
    let curr = head;
    let depth = 0;
    let lastCurr;
    for (let i = 0; i < m - 1; i++) {
        lastCurr = curr;
        curr = curr.next;
    }
    let tail = curr;
    while (depth < n - m) {
        let tmp = tail.next.next;
        tail.next.next = curr;
        curr = tail.next;
        tail.next = tmp;
        depth++;
    }
    lastCurr ? (lastCurr.next = curr) : head = curr;
    return head;
};

25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.


k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list's nodes, only nodes itself may be changed.

将链表分成长度为k的数段,反转每一段,只允许使用常量级别的额外空间。

var reverseKGroup = function (head, k) {
    if (k === 1) {
        return head;
    }
    let curr = head;
    let headTail,tailHead,rowHead;
    let count = 0;
    while (curr) {
        if (!rowHead) {
            rowHead = curr;
            curr = curr.next;
        } else if ((count + 1) % k === 0) {
            tailHead = curr.next;
            curr.next = null;
            let [kHead, kTail] = reverseinK(rowHead);
            if (headTail) {
                headTail.next = kHead;
                kTail.next = tailHead;
                headTail = kTail;
            } else {
                head = kHead;
                headTail = kTail;
                headTail.next = tailHead;
            }
            rowHead = null;
            curr = tailHead;
        } else {
            curr = curr.next;
        }
        count++;
    }
    return head;
};

var reverseinK = function (head) {
    let tail = head;
    while (tail.next) {
        let tmp = tail.next.next;
        tail.next.next = head;
        head = tail.next;
        tail.next = tmp;
    }
    return [head, tail]
}

相关文章

  • 三道反转链表

    206.Reverse Linked List Reverse a singly linked list. 简单题...

  • leetcode链表之反转链表

    本文主要有三道题,都是关于反转链表的算法题,由浅入深。文章出现的代码都是python3 206、反转链表[http...

  • 链表的反转

    反转链表是一道很基本的面试题,通过更改节点之间的链接来反转链表。 1.单链表的反转 题目 示例 用一幅图来解释:这...

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

  • 链表反转

    循环反转链表 递归反转链表

  • 反转链表

    反转链表是一道有关链表的训练题,虽然比较简单,但是比较能训练思维。反转链表的题目比较自然的思维是用迭代,比较容易实...

  • 5个链表的常见操作

    链表 链表反转 LeetCode206:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 环路检...

  • JZ-015-反转链表

    反转链表 题目描述 输入一个链表,反转链表后,输出新链表的表头。题目链接: 反转链表[https://www.no...

  • 正面刚算法-单链表反转(附记忆窍门)

    单链表反转 单链表反转通俗表达:链上的后置指针全部倒戈。大家集体向后转,链头变链尾。 一道很基础但很考验对于链表掌...

  • 算法学习(链表相关问题)

    LeetCode 206 反转链表 LeetCode 92 反转链表II (练习) 完成,方法:在反转链表上改 L...

网友评论

      本文标题:三道反转链表

      本文链接:https://www.haomeiwen.com/subject/pjpodctx.html