当前位置: 首页 > news >正文

leetcode: Swapping Nodes in a Linked List

leetcode: Swapping Nodes in a Linked List

  • 1. 题目描述
  • 2. 题目解答
  • 3. 总结

1. 题目描述

You are given the head of a linked list, and an integer k.Return the head of the linked list after swapping the values of the kth node 
from the beginning and the kth node from the end (the list is 1-indexed).

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]

Example 2:

Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]

Constraints:

The number of nodes in the list is n.
1 <= k <= n <= 105
0 <= Node.val <= 100

2. 题目解答

class Solution {
public:ListNode* swapNodes(ListNode* head, int k) {ListNode* dummy = new ListNode(0);dummy->next = head;ListNode* A_ = findNodeA_(dummy, k);cout<<A_->val<<endl;ListNode* A  = A_->next;ListNode* B_ = findNodeB_(dummy, k);ListNode* B  = B_->next;cout<<A_->val<<" "<<A->val<<" "<<B_->val<<" "<<B->val<<endl;if(B->next == A){ListNode* temp = A->next;B_->next = A;A->next = B;B->next = temp;}else if(A->next == B){ListNode* temp = B->next;A_->next = B;B->next = A;A->next = temp;}else{ListNode* temp = B->next;A_->next = B;B->next = A->next;B_->next = A;A->next = temp;}return dummy->next;}
private:ListNode* findNodeA_(ListNode* dummy, int k){ListNode* temp = dummy;while(k-- > 1){temp = temp->next;}return temp;}ListNode* findNodeB_(ListNode* dummy, int k){ListNode* temp_pre = dummy;ListNode* temp_after = dummy;while(k-- > 0){temp_pre = temp_pre->next;}while(temp_pre->next != nullptr){temp_pre = temp_pre->next;temp_after = temp_after->next;}return temp_after;}
};

3. 总结

  第一步:如果在已经做了leetcode: Remove Nth Node From End of List这道题目,或者在已经知道dummy节点trick的基础上,应该会有一个大概的思路,可以写出findNodeA_和findNodeB_。其中A_表示k-1th元素,A表示kth元素;B_表示倒数k+1th元素,B表示倒数kth元素。
  第二步:通过举一个特例,可以写出交换两个节点的代码:

A_->next = B;
B->next = A->next;
B_->next = A;
A->next = temp;

  在写第二步代码的时候

原始代码不成环的条件推论
A_->next = BA_ != BB->next != A
B->next = A->nextB != A->nextA->next != B
B_->next = AB_ != AA->next != B

  这也就是第三步写出如下代码的来源:

        if(B->next == A){ListNode* temp = A->next;B_->next = A;A->next = B;B->next = temp;}else if(A->next == B){ListNode* temp = B->next;A_->next = B;B->next = A;A->next = temp;}
http://www.lryc.cn/news/24528.html

相关文章:

  • Nydus 在约苗平台的容器镜像加速实践
  • 企业对不同形态CRM系统价格需求不同
  • 「JVM 高效并发」线程安全
  • 微信扫码登录
  • Unity协程的简单应用
  • LeetCode 1250. Check If It Is a Good Array【数论】
  • ETHDenver 2023
  • React架构演变
  • 安全认证--JWT介绍及使用
  • 【计算机组成原理】计算机硬件的基础组成、认识各个硬件部件
  • 使用ChIPSeeker进行ChIP-seq, ATAC-seq,cuttag等富集峰的基因组注释
  • 第九届蓝桥杯省赛——7缩位求和
  • 【c++】STL常用容器5—list容器
  • 【牛客刷题专栏】0x0D:JZ5 替换空格(C语言编程题)
  • 聚观早报 | 苹果2024年放弃高通;腾讯回应进军类 ChatGPT
  • Elasticsearch:如何正确处理 Elasticsearch 摄取管道故障
  • 指标体系—北极星指标体系
  • 【操作系统】内存管理
  • 家庭消耗品跟踪管理软件HomeLists
  • django模型简要(1)
  • 【shell 编程大全】sed详解
  • 关于sudo配置
  • EEGLAB处理运动想象脑电数据
  • span标签的使用场景
  • Kafka面试问题总结
  • FPGA案例开发手册——基于全志T3+Logos FPGA核心板
  • 或许你想要的画图工具在这里
  • 2023年功能测试还值得入行吗?
  • 2022-2023山东大学机器学习期末回忆及复习建议
  • 基于ssm框架实现家庭理财收支系统(源码+数据库+文档)