码迷,mamicode.com
首页 > 其他好文 > 详细

24. 两两交换链表中的节点

时间:2019-11-03 18:00:12      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:ever   描述   new   you   self   wap   example   pair   efi   

题目描述:

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. 两两交换链表中的节点

标签:ever   描述   new   you   self   wap   example   pair   efi   

原文地址:https://www.cnblogs.com/zjuhaohaoxuexi/p/11788096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!