cpongo
来源:互联网 发布:seo盒子破解版 编辑:程序博客网 时间:2024/06/10 03:32
翻译
<code class="hljs haskell has-numbering">给定一个链表,调换每两个相邻节点,并返回其头部。例如,给定 <span class="hljs-number">1</span>-><span class="hljs-number">2</span>-><span class="hljs-number">3</span>-><span class="hljs-number">4</span>, 你应该返回的链表是 <span class="hljs-number">2</span>-><span class="hljs-number">1</span>-><span class="hljs-number">4</span>-><span class="hljs-number">3</span>。你的算法必须使用唯一不变的空间。你也不能修改列表中的值,只有节点本身是可以改变的。</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>
原文
<code class="hljs applescript has-numbering">Give a linked <span class="hljs-type">list</span>, swap <span class="hljs-keyword">every</span> two adjacent nodes <span class="hljs-keyword">and</span> <span class="hljs-constant">return</span> <span class="hljs-keyword">its</span> head.For example, Given <span class="hljs-number">1</span>-><span class="hljs-number">2</span>-><span class="hljs-number">3</span>-><span class="hljs-number">4</span>, you should <span class="hljs-constant">return</span> <span class="hljs-keyword">the</span> <span class="hljs-type">list</span> <span class="hljs-keyword">as</span> <span class="hljs-number">2</span>-><span class="hljs-number">1</span>-><span class="hljs-number">4</span>-><span class="hljs-number">3.</span>Your algorithm should use only <span class="hljs-type">constant</span> <span class="hljs-constant">space</span>. You may <span class="hljs-keyword">not</span> modify <span class="hljs-keyword">the</span> values <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-type">list</span>, only nodes itself can be changed.</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>
分析
我们就以题目中给出的1,2,3,4作为示例,将其作为两部分
<code class="hljs brainfuck has-numbering"><span class="hljs-comment">1,2</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span> <span class="hljs-comment">3,4</span></code><ul style="display: block;" class="pre-numbering"><li>1</li></ul>
或者我画个图出来更加直观吧……
代码
<code class="hljs java has-numbering"><span class="hljs-javadoc">/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode* next; * ListNode(int x): val(x), next(NULL) {} * }; */</span>class Solution {<span class="hljs-keyword">public</span>: ListNode* <span class="hljs-title">swapPairs</span>(ListNode* head) { <span class="hljs-keyword">if</span>(head == NULL) <span class="hljs-keyword">return</span> NULL; <span class="hljs-keyword">if</span>(head->next == NULL) <span class="hljs-keyword">return</span> head; ListNode* temp = head->next; head->next = swapPairs(temp->next); temp->next = head; <span class="hljs-keyword">return</span> temp; }};</code>
0 0
- cpongo
- python 网页爬虫_正则匹配
- 常用icon以及color颜色RGB值和对应颜色效果图
- 12.3 实现一个剪贴板查看器
- 牛客网 | 孩子们的游戏(圆圈中最后剩下的数)
- 第一次使用csdn博客
- cpongo
- HDU 2546 饭卡
- 微软100道算法题-----输入一个已经按升序排序过的数组和一个数字, 在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对
- Linux NFS服务器的安装与配置
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- json datatime convert
- Android开发 屏幕适配
- Android基础入门教程——10.8 LayoutInflater(布局服务)
- 第二天 Python列表的使用