博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Swap Nodes in Pairs
阅读量:4331 次
发布时间:2019-06-06

本文共 2624 字,大约阅读时间需要 8 分钟。

2013.12.7 01:54

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.

Solution:

  This kind of problem is designed just to make some trouble for you, and the interviewer must be very glad watching you getting into a bloody mess with the code. So let's first write down the process in language description:

    1. use two pointers $p1/$p2 pointing to the first/second node in each pair.

    2. change the $next pointers of p1 and p2

    3. swap $p1 and $p2, make sure $p1 is still before $p2.

    4. record current $p2 as the parent pointer $par, which will be used in the next pair.

    5. move both $p1 and $p2 forward by two steps, don't forget to check for nullptr.

    6. {do the swapping for the next pair}, and point $par->next to $p1

  Time complexiy is O(n), space complexity is O(1). The real problem is whether you can write the code nice and clean. Try to be careful and patient.

Accepted code:

// This problem is tricky, 3WA/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode *swapPairs(ListNode *head) {        // IMPORTANT: Please reset any member data you declared, as        // the same Solution instance will be reused for each test case.        ListNode *p1 = nullptr, *p2 = nullptr;                if(head == nullptr){            return head;        }                p1 = head;        p2 = head->next;        if(p2 == nullptr){            return p1;        }                bool first_swap = true;        ListNode *tmp, *par;        ListNode *root;                par = new ListNode(0);        root = par;        par->next = p1;        while(true){            p1->next = p2->next;            p2->next = p1;                        tmp = p1;            p1 = p2;            p2 = tmp;                        par->next = p1;            par = p2;                        if(first_swap){                head = p1;                first_swap = false;            }                        p1 = p1->next;            p2 = p1->next;            if(p2 == nullptr){                break;            }                        p1 = p1->next;            p2 = p1->next;            if(p2 == nullptr){                break;            }        }                delete root;        root = nullptr;        return head;    }};

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3462391.html

你可能感兴趣的文章
C#中使用AOP
查看>>
在CANopen网络中通过LSS服务设置节点地址和网络波特率
查看>>
sql server 查询及删除重复记录的方法【转载】
查看>>
zTree多级扩展(一)
查看>>
Android学习二_八:Animation的使用(一) (转)
查看>>
Miller-Rabin
查看>>
VS中MFC连接MySQL的方法【转】
查看>>
PHP基础(二)
查看>>
lvm逻辑卷扩展方法
查看>>
JAVA锁
查看>>
C语言程序的内存分配方式
查看>>
将硬盘从FAT32转化为NTFS以支持everything搜索
查看>>
2、JAVA基础- 关键字、标识符、常变量、数据类型、注释等
查看>>
form表单上传图片格式
查看>>
颜色追踪块CamShift---33
查看>>
c++字符串变量---8
查看>>
phpcms V9首页 频道页 列表页 推荐位 简单获取文章浏览量和评论统计
查看>>
Navicat 报错1251连接不成功Mysql
查看>>
【新年福利】《正则表达式30分钟入门》APP版本发布
查看>>
R语言排序函数汇总
查看>>