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

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

  1. Swap Nodes in Pairs My Submissions QuestionEditorial Solution
    Total Accepted: 95230 Total Submissions: 270562 Difficulty: Easy
    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

思路:简单,学会指针间的操作和指向

/** * 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) {        if(head==NULL||head->next==NULL)return head;        ListNode *beg = head->next,*p=head,*pre=head;        while(p!=NULL&&p->next!=NULL)        {            ListNode *p_next_next = p->next->next;            pre->next = p->next;            p->next->next = p;            p->next = p_next_next;            pre = p;            p = p->next;        }        return beg;    }};

转载于:https://www.cnblogs.com/freeopen/p/5482935.html

你可能感兴趣的文章
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>