16_leetcode_swap node in pairs
来源:互联网 发布:大数据技术研究方向 编辑:程序博客网 时间:2024/05/16 16:56
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.
1:链表为NULL或者链表只有一个节点;2:首先交换头两个节点,确定头节点和尾节点;3:循环交换后两个节点;4当末尾节点个数不大于1的时候,直接添加到当前链表的尾部。
ListNode* swapPairs(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode* temp = head->next; head->next = temp->next; temp->next = head; head = temp; ListNode* index = head->next; temp = index->next; while(temp && temp->next) { index->next = temp->next; index = index->next; temp->next = index->next; index->next = temp; index = index->next; temp = index->next; } index->next = temp; return head; }
0 0
- 16_leetcode_swap node in pairs
- leetcode_swap node in pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes In Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- oracle学习之bulk collect用法
- 两分钟学会Unity3D布娃娃的使用
- Contiki:一个鲜为人知的、主宰物联网的开源操作系统
- LeetCode: Path Sum [112]
- C/C++堆、栈及静态数据区详解
- 16_leetcode_swap node in pairs
- java-IO操作性能对比
- 存储过程的优缺点及使用场景
- Java内存管理简析
- 如何在Ubuntu14.04上运行32位程序
- Java 守护线程
- 开发者揭秘Swift语言有哪些优点?
- 17_leetcode_merge k Sorted List
- Myecplise 8.6 注册代码