24. 两两交换链表中的节点
Posted zjuhaohaoxuexi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了24. 两两交换链表中的节点相关的知识,希望对你有一定的参考价值。
题目描述:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given1->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) {} * }; */
解法1:
递归方法
代码:
class Solution { public: ListNode *swapPairs(ListNode *head) { if(head==nullptr || head->next==nullptr) return head; ListNode *next = swapPairs(head->next->next); ListNode *newHead = head->next; newHead->next = head; head->next = next; return newHead; } };
解法2:
直接方法
代码:
class Solution { public: ListNode *swapPairs(ListNode *head) { if(head==nullptr || head->next==nullptr) return head; ListNode dummy = ListNode(-1); ListNode *last = &dummy; ListNode *cur = head; while(cur!=nullptr && cur->next!=nullptr) { ListNode *next = cur->next->next; last->next = cur->next; last->next->next = cur; last->next->next->next = nullptr; last = last->next->next; cur = next; } if(cur!=nullptr) last->next = cur; return dummy.next; } };
以上是关于24. 两两交换链表中的节点的主要内容,如果未能解决你的问题,请参考以下文章
代码随想录算法训练营第四天 | 24.两两交换链表中的节点19.删除链表的倒数第N个节点160.相交链表142.环形链表II