博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swap Nodes in Pairs --成对交换链表节点(重重重)
阅读量:4107 次
发布时间:2019-05-25

本文共 693 字,大约阅读时间需要 2 分钟。

题目:

解答:

对链表的考察  算是比较经典的一道题目

循环取出未处理的链表的头两个节点,交换位置,链接到已处理节点的尾部。

这里要注意的是last节点,cur节点  nextcur节点的更新。

在纸上多画画,就可以清楚了。

这道题需要再做一遍,加深印象。

代码:

class Solution {public:	ListNode *swapPairs(ListNode *head) {		ListNode *cur = head;		ListNode *nextcur;		ListNode *last;		if (head == NULL)			return head;		if (head->next == NULL)			return head;		cur = head;		head = head->next;		nextcur = head->next;		head->next = cur;		cur->next = nextcur;		cur = nextcur;		last = head->next;		while (1)		{			if (cur == NULL)				return head;			if (cur->next == NULL)				return head;			nextcur = cur->next->next;			cur->next->next = cur;			last->next = cur->next;			last = cur;			cur->next = nextcur;			cur = nextcur;					}	}};

转载地址:http://mutsi.baihongyu.com/

你可能感兴趣的文章
JavaScript实现页面无刷新让时间走动
查看>>
前端设计之特效表单
查看>>
Java的时间操作玩法实例若干
查看>>
JavaScript:时间日期格式验证大全
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
(python版)《剑指Offer》JZ28:数组中出现次数超过一半的数字
查看>>
(python版)《剑指Offer》JZ30:连续子数组的最大和
查看>>
管理用户状态——Cookie与Session
查看>>
通过Spring Boot三分钟创建Spring Web项目
查看>>
Java编程基础:static的用法
查看>>
Java编程基础:抽象类和接口
查看>>
Java编程基础:异常处理
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
Spring处理表单提交
查看>>
Spring MVC异常处理
查看>>
PHP 7 的五大新特性
查看>>
PHP底层的运行机制与原理
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>