Swap Nodes in Pairs
2015-08-03 21:40
651 查看
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
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.
public class Solution { public ListNode swapPairs(ListNode head) { if(head == null || head.next == null) { return head; } ListNode point = new ListNode(0); point.next = head; head = point; while(point.next != null && point.next.next != null) { ListNode temp = point.next.next.next; point.next.next.next = point.next; point.next = point.next.next; point.next.next.next = temp; point = point.next.next; } return head.next; } }
相关文章推荐
- Remove Nth Node From End of List
- NodeJS 常用模块整理
- node在windows下node-gyp编译可能会出现的问题
- 那些年,在nodejs上踩过的坑(一)
- Node.js-小心buffer的拼接问题
- node.js学习笔记
- NodeJS学习第一季-Markdown模块
- 19.Remove Nth Node From End of List(移除单链表中倒数第N个结点)
- Node.js学习入门手册
- Count Complete Tree Nodes***
- [LeetCode] Count Complete Tree Nodes
- Windows下Node.js开发入门(1)
- node-zk-browser环境搭建
- [leetcode] 222.Count Complete Tree Nodes
- Leetcode #222 Count Complete Tree Nodes
- Leetcode Delete Node in a Linked List
- node.js addons development Prerequisite knowledgement/node.js插件开发预备知识
- Reverse Nodes in k-Group
- Reverse Nodes in k-Group
- 理解inode