[leetcode][list] Swap Nodes in Pairs
来源:互联网 发布:linux服务器增加硬盘 编辑:程序博客网 时间:2024/05/16 10:26
题目:
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.
/** * 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(NULL == head || NULL == head->next) return head; ListNode *headNew = head->next;//指向新链表的第一个节点 ListNode *pFirst = head;//指向本节点对的第一个节点 ListNode *pSecond = head->next;//指向本节点对的第二个节点 ListNode *pNext = head->next->next;//指向本节点对的下一个节点 ListNode *pLast = head;//指向新链表的最后一个节点 pSecond->next = pFirst; while(pNext && pNext->next){ pFirst = pNext; pSecond = pFirst->next; pNext = pSecond->next; pSecond->next = pFirst; pLast->next = pSecond; pLast = pFirst; } if(pNext) pLast->next = pNext; else pLast->next = NULL; return headNew; }};
以下是无视题目中要求不能修改节点val的版本
/** * 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(NULL == head || NULL == head->next) return head; ListNode *pOdd(head), *pEven(head->next); while(pOdd && pOdd->next && pEven->next){//!!!先保证pOdd不为NULL撒 int iTmp = pOdd->val;//交换两个节点的val pOdd->val = pEven->val; pEven->val = iTmp; pOdd = pOdd->next->next;//更新pOdd pEven = pEven->next->next;//更新pEven } if(NULL == pOdd ->next) return head;//节点个数为奇数 //节点个数为偶数 int iTmp = pOdd->val; pOdd->val = pEven->val; pEven->val = iTmp; return head;//节点没变,只是改变了节点的val,所以head没变 }};
递归实现:
/** * 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 (NULL == head || NULL == head->next) return head;ListNode *p = head->next;ListNode *q = p->next;p->next = head;head->next = swapPairs(q);return p;}};
0 0
- [leetcode][list] Swap Nodes in Pairs
- 24. Swap Nodes in Pairs leetcode(list)
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- 【LeetCode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- MySQL索引背后的数据结构及算法原理
- 分布式系统互斥算法---分布式算法
- 软件下载 itellyou
- Hbase安装过程中遇到的问题
- leetcodeBinary Tree Inorder Traversal
- [leetcode][list] Swap Nodes in Pairs
- 斐波那契的两种实现方法
- thinkphp verify验证码使用
- android.view.InflateException: Binary XML file line #7: Error inflating class
- 数据库的最简单实现
- c 语言常用基础语句
- Window下使用eclipse连接hadoop报错(二)
- nyoj题目277:车牌号
- 网页上不带下载链接但可播放的歌曲可以如何下载呢?