leetcode_24_Swap Nodes in Pairs

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢

Swap Nodes in Pairs

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.

//测试Accepted
//要注意的一点是一旦是把A和B交换了以后,A的父节点就指向了B。这个是容易忘记的一点。
class Solution {
public:
	ListNode *swapPairs(ListNode *head) {
		ListNode* prev = NULL;
		ListNode* first = NULL;
		ListNode* second = head;
		int k = 1;
		while (second != NULL)
		{
			if (k%2 == 0)
			{
				first->next = second->next;
				second->next = first;
				if(prev != NULL)//when process prev pointer, we should be very careful
					prev->next = second;
				else head = second;
				ListNode* tmp = first;
				first = second;
				second = tmp;
			}
			prev = first;
			first = second;
			second = second->next;
			k++;
		}
		return head;
	}
};
//要注意的一点是一旦是把A和B交换了以后,A的父节点就指向了B。这个是容易忘记的一点。
#include<iostream>

using namespace std;

#define N 5

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
	ListNode *swapPairs(ListNode *head) {
		ListNode* prev = NULL;
		ListNode* first = NULL;
		ListNode* second = head;
		int k = 1;
		while (second != NULL)
		{
			if (k%2 == 0)
			{
				first->next = second->next;
				second->next = first;
				if(prev != NULL)//when process prev pointer, we should be very careful
					prev->next = second;
				else head = second;
				ListNode* tmp = first;
				first = second;
				second = tmp;
			}
			prev = first;
			first = second;
			second = second->next;
			k++;
		}
		return head;
	}
};

ListNode *creatlist()
{
	ListNode *head;
	head=NULL;

	for(int i=0; i<N; i++)
	{
		int a;
		ListNode *p;
		cin>>a;
		p = (ListNode*)malloc(sizeof(ListNode));
		p->val=a;
		p->next=head;
		head = p;
	}
	return head;
}

int main()
{
	ListNode *list=creatlist();
	Solution lin;
	ListNode *outlist=lin.swapPairs(list);

	for(int i=0; i<N; i++)
	{
		cout<<outlist->val;
		outlist = outlist->next;
	}
}
时间: 2024-08-13 22:29:25

leetcode_24_Swap Nodes in Pairs的相关文章

Leetcode-24 Swap Nodes in Pairs

#24. Swap Nodes in Pairs 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 t

[LintCode] Swap Nodes in Pairs 成对交换节点

Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you should return the list as 2->1->4->3. Challenge Your algorithm should use only constant space. You may not modify the values in the lis

LeetCode: Swap Nodes in Pairs 解题报告

Swap Nodes in Pairs 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 val

63. Swap Nodes in Pairs &amp;&amp; Rotate List &amp;&amp; Remove Nth Node From End of List

Swap Nodes in Pairs 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 va

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

每日算法之二十二:Swap Nodes in Pairs

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, on

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: 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 va

【LeetCode】Swap Nodes in Pairs 链表指针的应用

题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pairs * 题目:输入一个链表,要求将链表每相邻的两个节点交换位置后输出 * 思路:遍历一遍就可以,时间复杂度O(n) * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Li

leetCode 24. Swap Nodes in Pairs 链表

24. Swap Nodes in Pairs 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