LintCode "Swap Two Nodes in Linked List"

Nothing special. Just take care of corner cases.

class Solution {
public:
    /**
     * @param head a ListNode
     * @oaram v1 an integer
     * @param v2 an integer
     * @return a new head of singly-linked list
     */
    ListNode* swapNodes(ListNode* head, int v1, int v2)
    {
        if(!head) return head;

        ListNode *p1 = nullptr, *p1p = nullptr, *p1n = nullptr;
        ListNode *p2 = nullptr, *p2p = nullptr, *p2n = nullptr;

        //  Pass 1: Find nodes
        //
        ListNode *prev = nullptr, *p = head, *next = p->next;
        while(p)
        {
            if(p->val == v1 || p->val == v2)
            {
                if(!p1)
                {
                    p1 = p;
                    p1p = prev;
                    p1n = next;
                }
                else
                {
                    p2 = p;
                    p2p = prev;
                    p2n = next;
                }
            }
            // move on
            prev = p;
            p = next;
            next = next?next->next:nullptr;
        }// while

        if(!p1 || !p2)
            return head;

        //  Step 2:
        //
        ListNode *ret = head;
        if(p1 == head)
        {
            ret = p2;
        }

        if (p1n == p2) // adjacent
        {
            if(p1p)
                p1p->next = p2;
            p2->next = p1;
            p1->next = p2n;
        }
        else
        {
            if(p1p)
                p1p->next = p2;
            p2->next = p1n;
            p2p->next = p1;
            p1->next = p2n;
        }

        return ret;
    }
};

时间: 2024-11-08 10:04:00

LintCode "Swap Two Nodes in Linked List"的相关文章

[LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing. Notice You sh

Swap Two Nodes in Linked List

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing. Notice You sh

[LintCode] 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. Challenge Your algorithm should use only constant space. You may not modify the values in the lis

lintcode-medium-Swap Two Nodes in Linked List

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It's guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing. Notice You sh

[LeetCode 1171] Remove Zero Sum Consecutive Nodes from Linked List

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. After doing so, return the head of the final linked list.  You may return any such answer. (Note that in the example

leeetcode1171 Remove Zero Sum Consecutive Nodes from Linked List

1 """ 2 Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. 3 4 After doing so, return the head of the final linked list. You may return any such answer. 5 6

[LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the next pointer in ListNode. Notice Don't forget to mark the left child of each node to null. Or you will get Time Limit Exceeded

lintcode 容易题:Reverse Linked List 翻转链表

题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还是可以解的 java程序: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = nu

lintcode 中等题:Palindrome Linked List 回文链表

题目 回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 解题 法一: 再定义一个链表,存放链表反转的值,再以此比较两个链表中的值是否相等,时间复杂度O(N),空间复杂度O(N) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v