24. Swap Nodes in Pairs
来源:互联网 发布:golang http 断点续传 编辑:程序博客网 时间:2024/04/29 15:05
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.
关于list的操作,下面是我的错误答案,input [1,2],output[1],excepted[2,1]。
public class Solution { public ListNode swapPairs(ListNode head) { if (head == null) { return null; } ListNode headlist = new ListNode(0); ListNode templist = new ListNode(0); headlist.next = head; while (head.next != null) { templist.next = head.next.next; head.next.next = head; head.next = templist.next; head = head.next; if (head == null) { break; } if (head.next == null) { break; } } return headlist.next; }}下面是正确的做法:
public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null && current.next.next != null) { ListNode first = current.next; ListNode second = current.next.next; first.next = second.next; current.next = second; current.next.next = first; current = current.next.next; } return dummy.next;}可以看到我是直接对形参head操作,而正确做法是新建current,first,second,对所有要用到的listnode都新建,暂时还不知道为什么这么做。
知道了,这里不可以改head,因为代码中把headlist.next设成head,就不要动head了,否则结果会错。不过上面的代码明显有个瑕疵:
public class Solution { public ListNode swapPairs(ListNode head) { ListNode headlist = new ListNode(0); ListNode curlist = new ListNode(0); headlist.next = head; curlist = headlist; while (curlist.next != null && curlist.next.next != null) { ListNode first = curlist.next; ListNode second = curlist.next.next; first.next = second.next; second.next = first; //这里应该改变第二个元素的next啊,不知道系统为什么认为不加也对 curlist.next = second; curlist.next.next = first; curlist = curlist.next.next; } return headlist.next; }}
这道题的递归解法也很奇妙,先记录下原本的head.next,然后head.next 其实应该等于swamPairs(head.next.next),然后把原本的head.next.next设成head,代码如下:
public class Solution { public ListNode swapPairs(ListNode head) { if ((head == null)||(head.next == null)) return head; ListNode n = head.next; head.next = swapPairs(head.next.next); n.next = head; return n; }}
0 0
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- linux下mongodb的副本集搭建(replica Set)
- 图片轮播插件-基于HTML5+CSS3+JavaSript的响应式图片轮播
- 自定义ImageView,展示gif图或者利用帧动画展示动图
- python常用的一些东西——sys、os
- ZCMU-1775-xx
- 24. Swap Nodes in Pairs
- drools-kie API 和kmodule.xml 文件详解
- 《年轻时,你最该拒绝什么?-王锋》
- Android Studio自动生成选择器插件SelectorChapek
- https 配置指南
- ThreadPoolExecutor线程池的使用与理解
- Leetcode oj java 54. Spiral Matrix
- 复制文件显示进度
- android webview简单加载echart表格(折线,饼形,柱形)