首页 > 其他分享 >【LeetCode 0024】【链表】交换单链表相邻两个节点

【LeetCode 0024】【链表】交换单链表相邻两个节点

时间:2024-07-07 14:00:16浏览次数:18  
标签:current head val 0024 next 链表 nodes prev LeetCode

  1. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)

Example 1:

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

Example 2:

**Input:** head = []
**Output:** []

Example 3:

**Input:** head = [1]
**Output:** [1]

Constraints:

  • The number of nodes in the list is in the range [0, 100].
  • 0 <= Node.val <= 100
Idea
使用三个指针迭代整条链表
current/next 表示待交换的2个节点指针
prev 表示比较2个节点的第一个节点的前一个节点
JavaScript Solution
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function(head) {
    let dummyHead = new ListNode(-1,head)
    let prev = dummyHead
    while(prev && prev.next && prev.next.next){
      let current = prev.next
      let next = prev.next.next.next
      prev.next = prev.next.next
      prev.next.next = current
      current.next = next
      prev = current
    }
    return dummyHead.next
};

标签:current,head,val,0024,next,链表,nodes,prev,LeetCode
From: https://blog.csdn.net/avenccssddnn/article/details/140245187

相关文章

  • 【LeetCode 0141】【链表】【双指针之快慢指针】判断给定单链表是否存在环
    LinkedListCycleGivenhead,theheadofalinkedlist,determineifthelinkedlisthasacycleinit.Thereisacycleinalinkedlistifthereissomenodeinthelistthatcanbereachedagainbycontinuouslyfollowingthe next pointer.Internal......
  • 手绘图系列 01 | 链表到底是什么?
    引言程序=数据结构+算法,在编写代码时,选择一个合适的数据结构是成功的一半。不过,问题是:解决同一个问题有很多种方法。对应到组织数据时,有很多数据结构都可以完成指定的工作。关键在于知道使用哪种数据结构是正确且最高效的。很长一段时间对我来说,链表一直是其中一个比......
  • [LeetCode] 1366. Rank Teams by Votes 通过投票对团队排名
    Inaspecialrankingsystem,eachvotergivesarankfromhighesttolowesttoallteamsparticipatinginthecompetition.Theorderingofteamsisdecidedbywhoreceivedthemostposition-onevotes.Iftwoormoreteamstieinthefirstposition,wecon......
  • 代码随想录算法训练营第四天 | 24. 两两交换链表中的节点 、 19.删除链表的倒数第N个
    24.两两交换链表中的节点 题目:.-力扣(LeetCode)思路:这题关键是要每次进行两个结点的操作,并且每次都要保存其前结点,做题思路比较清晰,但是总是处理不好边界问题,总是越界。代码:/***Definitionforsingly-linkedlist.*structListNode{*intval;*List......
  • 代码随想录算法训练营第三天 | 203.移除链表元素 、 707.设计链表 、206.反转链表
    203.移除链表元素题目:.-力扣(LeetCode)思路:主要是通过运用虚拟头节点来统一移除元素的写法。代码:/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListNode......
  • 代码随想录刷题day 4 | 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题
    24.两两交换链表中的节点迭代的版本:最重要的就是要知道循环变量怎么取,对于这道题,我们只需要存储需要交换的两个节点的前一个节点即可,只有当这个节点后面有两个节点时才进入循环,其实把握住这一点之后这题就非常容易了。递归的版本:这道题用递归做简直不要太简单,首先明白递归结束......
  • leetcode 257. 二叉树的所有路径
    给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。叶子节点 是指没有子节点的节点。 示例1:输入:root=[1,2,3,null,5]输出:["1->2->5","1->3"]示例2:输入:root=[1]输出:["1"]java解题思路及代码实现递归法packagecom.java......
  • leetcode 102. 二叉树的层序遍历
    给你二叉树的根节点 root ,返回其节点值的 层序遍历 。(即逐层地,从左到右访问所有节点)。示例1:输入:root=[3,9,20,null,null,15,7]输出:[[3],[9,20],[15,7]]示例2:输入:root=[1]输出:[[1]]示例3:输入:root=[]输出:[]提示:树中节点数目在范围 [0,2000] ......
  • leetcode77组合——经典回溯算法
    本文主要讲解组合的要点与细节,以及回溯算法的解题步骤,按照步骤思考更方便理解 c++和java代码如下,末尾给定两个整数 n 和 k,返回范围 [1,n] 中所有可能的 k 个数的组合。你可以按 任何顺序 返回答案。 具体要点:1.首先,这道题的暴力解法是k层for循环,遍历所有的......
  • 数据结构——(双)链表
    文章目录1. 定义2. 双链表和单链表的区别3.代码示例3.1双链表节点和结构定义3.2初始化双链表3.3 返回双链表的长度3.4 在指定位置插入元素3.5 在末尾插入元素3.6 删除指定位置的元素并返回被删除的元素3.7 删除末尾元素3.8获取指定位置的元素3.9修改指......