Leetcode----Swap Nodes in Pairs

Source: Internet
Author: User

Title:

Given a linked list, swap every, 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. Modify the values in the list, only nodes itself can be changed.

Main topic:given a linked list, two nodes adjacent to the linked list are exchanged to return the linked list after the exchange. Ideas:The subject is relatively simple, there are two ways to solve the problem: the value of the Exchange node, the point of the switching node. 1, the value of the Exchange node: The simplest way is to exchange the value of two nodes, to achieve the purpose of the exchange. 2, the exchange of neighboring nodes point, when p2 = P1->next when the execution p1->next = P2->next,p2->next = P1. However, in the process of exchange, it is also ensured that the precursor node of the P2 is the precursor of the P1 before the exchange. 3, or the idea of switching nodes, but this time is not to consider the direction of the cycle of exchange, but through the return exchange. This procedure of recursion is concise and the focus is on the different ways of thinking. The recursive way of thinking in this problem is to consider a pair of situations, and then apply this analysis to the back node, recursively, and solve the result. Code:Change Value:
listnode* Swappairs (listnode* head) {        ListNode *p1 = head, *p2;        int tmp;                while (p1) {            P2 = p1->next;            if (p2) {                tmp = p1->val;                P1->val = p2->val;                P2->val = tmp;                P1 = p1->next;            }            P1 = p1->next;        }                return head; }
To change the pointer:
listnode* Swappairs (listnode* head) {        ListNode *p1 = head, *P2, *tmp;                if (P1 && p1->next) {            head = head->next;        }                TMP = head;                while (p1) {            P2 = p1->next;            if (p2) {                p1->next = p2->next;                P2->next = p1;                if (tmp! = head)                    tmp->next = p2;                TMP = p1;            }            P1 = p1->next;        }                return head;}
change Pointer (recursive version):
listnode* Swappairs (listnode* head) {        listnode* p1;               if (head && head->next) {              P1 = head->next;             Head->next = Swappairs (head->next->next);            P1->next = head;            head = p1;        }       return head;}

Leetcode----Swap Nodes in Pairs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.