Swap Nodes in Pairs leetcode

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.

Subscribe to see which companies asked this question

未优化的代码,用3个临时指针

ListNode* swapPairs(ListNode* head) {
    if (head == nullptr || head->next == nullptr)
        return head;
    ListNode *pre = head;
    ListNode *cur = pre->next;
    ListNode *net = cur->next;
    head = cur;
    while (true)
    {
        cur->next = pre;
        if (net == nullptr || net->next == nullptr) {
            pre->next = net;
            break;
        }
        pre->next = net->next;
        pre = net;
        cur = pre->next;
        net = cur->next;
    }
    return head;
}

惊人的解法,用双重指针,开阔思路,本以为自己可以随心所欲使用双指针,看来还是差得远

ListNode *swapPairs(ListNode *head) {
    ListNode **p = &head;

    while (*p && (*p)->next) {
        ListNode *t = (*p)->next;

        (*p)->next = t->next;
        t->next = *p;
        *p = t;

        p = &(*p)->next->next;
    }

    return head;
}
时间: 2024-08-25 23:13:15

Swap Nodes in Pairs leetcode的相关文章

Swap Nodes in Pairs leetcode java

题目: 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.http://i.cnblogs.com/EditPosts.aspx?opt=1 Your algorithm should use only constant space.

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

Leetcode: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】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——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, on

LeetCode: Swap Nodes in Pairs [023]

1. create a new accout, create orginazation, create repo 2. install git in your local pc Note: you can create ssh key to avoid username/password input for github operation https://help.github.com/articles/generating-ssh-keys https://help.github.com/a

leetcode | Swap Nodes in Pairs in a linklist

Swap Nodes in Pairs : https://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 should return the list as 2->1->4->3. Your algorithm shou

【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: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, on