首页 > 其他分享 >LeetCode刷题记录.Day5

LeetCode刷题记录.Day5

时间:2022-11-04 00:23:58浏览次数:72  
标签:head ListNode cur fastNode Day5 varHead next LeetCode 刷题

反转链表

题目链接206. 反转链表 - 力扣(LeetCode)

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* temp;
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur){
            temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};

 

经过几天的坚持刷题下来,双指针法感觉很简单。但是递归法还没太明白,准备周末再消化消化。

附上双指针法记录

两两交换链表中的节点

24. 两两交换链表中的节点 - 力扣(LeetCode)

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* varHead = new ListNode(0);
        varHead->next = head;
        ListNode* cur = varHead;
        while(cur->next != nullptr && cur->next->next != nullptr){
            ListNode* temp = cur->next;
            ListNode* temp1 =  cur->next->next->next;
            cur->next = cur->next->next;
            cur->next->next = temp;
            cur->next->next->next = temp1;
            cur = cur->next->next;
        }
        return varHead->next;

    }
};

跟着随想录用的虚拟头结点法。实际上写交换的时候想了一会儿 感觉有点麻烦,理了半天。看到了递归的方法。空下来仔细研究一下.

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head == NULL || head->next == NULL){
            return head;
        }
        ListNode* node = head->next;
        ListNode* next = node->next;
        node->next = head;
        head->next = swapPairs(next);
        return node;
    }
};

递过的方法。对于递归一直不太明白。

 

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

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* varHead = new ListNode(0);
        varHead->next = head;
        ListNode* fastNode = varHead;
        ListNode* slowNode = varHead;
        while(n-- && fastNode != NULL ){
            fastNode = fastNode->next;
        }
        fastNode = fastNode->next;
        while(fastNode != NULL){
            fastNode = fastNode->next;
            slowNode = slowNode->next;
        }
        slowNode->next = slowNode->next->next;

        return varHead->next;
    }
};

很典型的双指针法。细节上没注意。第一个循环完毕之后快指针要再移动一格,这样慢指针就不会刚好指向要删除的元素而是他的上一个。

标签:head,ListNode,cur,fastNode,Day5,varHead,next,LeetCode,刷题
From: https://www.cnblogs.com/tianmaster/p/16856347.html

相关文章

  • leetcode java 杨辉三角
    简介杨辉三角是一道简单题,可以通过类似一层推下一层的方式进行计算,但是好像看过一个题解,采用的方式是组合数。本来想采用组合数,尝试了double溢出尝试了long溢出,尝试......
  • LeetCode_Stack_589. N-ary Tree Preorder Traversal N 叉树的前序遍历【栈,迭代】【简
    目录​​一,题目描述​​​​英文描述​​​​中文描述​​​​示例与说明​​​​二,解题思路​​​​三,AC代码​​​​C++​​​​Java​​​​四,解题过程​​​​第一博​......
  • python描述 LeetCode 1486. 数组异或操作
    python描述LeetCode1486.数组异或操作  大家好,我是亓官劼(qíguānjié),在【亓官劼】公众号、GitHub、B站、华为开发者论坛等平台分享一些技术博文,主要包括前端开发、......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:二叉树的最大深度
    题目:给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,......
  • 计算机二级python备考刷题知识点总结(二)
    1、center()语法:str.center(width,fillchar)注:fillchar必须要用引号引起了center()返回一个原字符串居中,并使用填充字符填充到长度为width的新字符串,默认填充字符为空格......
  • LeetCode 283 Move Zeroes
    Givenanintegerarraynums,moveall0'stotheendofitwhilemaintainingtherelativeorderofthenon-zeroelements.Notethatyoumustdothisin-placew......
  • [LeetCode] 1668. Maximum Repeating Substring
    Forastring sequence,astring word is k-repeating if word concatenated k timesisasubstringof sequence.The word's maximum k-repeatingvalue......
  • leetcode - 94. 二叉树的中序遍历
    94.二叉树的中序遍历List<Integer>inorder=newArrayList<>();publicList<Integer>inorderTraversal(TreeNoderoot){wit......
  • leetcode257-二叉树的所有路径
    257.二叉树的所有路径 泪目,自己写出的递归遍历./***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*......
  • leetcode88
    合并两个有序数组Category Difficulty Likes Dislikesalgorithms Easy(52.02%) 1623 -TagsCompanies给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个......