LeetCode Solutions : 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, only nodes itself can be changed.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null)
            return head;
        ListNode faked=new ListNode(0);
        faked.next=head;
        ListNode pre=faked;
        ListNode cur=head;
        while(cur!=null&&cur.next!=null){
            pre.next=cur.next;
            cur.next=pre.next.next;
            pre.next.next=cur;
            pre=cur;
            cur=pre.next;
        }
        return faked.next;
    }
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-08-09 07:19:05

LeetCode Solutions : Swap Nodes in Pairs的相关文章

【LeetCode】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,

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 024. Swap Nodes in Pairs (Medium) 链接: 题目:https://oj.leetcode.com/problems/swap-nodes-in-pairs/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表中的每一对节点对换

【LeetCode】Swap Nodes in Pairs (3 solutions)

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 val

【LeetCode】Swap Nodes in Pairs 链表指针的应用

题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pairs * 题目:输入一个链表,要求将链表每相邻的两个节点交换位置后输出 * 思路:遍历一遍就可以,时间复杂度O(n) * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Li

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 024 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 th

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

(Java) LeetCode 24. Swap Nodes in Pairs —— 两两交换链表中的节点

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

leetcode 【 Swap Nodes in Pairs 】python 实现

题目: 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,