24. Swap Nodes in Pairs(js)

24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list‘s nodes, only nodes itself may be changed.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.题意:链表相邻节点调换位置代码如下:
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function(head) {
        let phead=new ListNode(0);
        phead.next=head;
        let curr=phead;

        while(curr.next && curr.next.next){
            let left=curr.next;
            let right=curr.next.next;

            left.next=right.next;
            curr.next=right;
            right.next=left;
            curr=curr.next.next;
        }
        return phead.next;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10387390.html

时间: 2024-10-09 03:07:58

24. Swap Nodes in Pairs(js)的相关文章

leetCode 24. Swap Nodes in Pairs 链表

24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the

[Leetcode][Python]24: Swap Nodes in Pairs

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 24: Swap Nodes in Pairshttps://oj.leetcode.com/problems/swap-nodes-in-pairs/ Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you shoul

leetcode_1题——Swap Nodes in Pairs(线性表的链式存储)

Swap Nodes in Pairs Total Accepted: 45110 Total Submissions: 138992My Submissions Question Solution Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4-

leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

【leetcode】24. Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. 解题分析: 解题思路很简单,就是先两两扫描,然后调换顺序即可.这道题的难点在于每个节点的next域指向问题,所以解这样的题最好可以在纸上写一下交换的步骤就不难实现 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val

24. Swap Nodes in Pairs Java solutions

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

LeetCode 24 Swap Nodes in Pairs (C,C++,Java,Python)

Problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the

[leedcode 24] Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

24. Swap Nodes in Pairs

1 class Solution { 2 public: 3 ListNode* swapPairs(ListNode* head) { 4 ListNode *t1,*t2,*t3,*t4; 5 if(head==NULL||head->next==NULL) 6 return head; 7 t4->next=t2->next; 8 t3=t2->next->next; 9 t2->next->next=t2; 10 t2->next=t3; 11 wh