2 Star 0 Fork 0

Mr.Z / bigsai-algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LeetCode 24两两交换链表中的节点.md 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
张赛 提交于 2020-11-26 10:08 . update

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

image-20201117202402020 分析: 本题的要求就是交换奇偶节点。不能直接交换值也就意味不能直接赋值要通过链表插入删除来实现。而具体的流程也很简单: 在这里插入图片描述

具体实现代码为:

public ListNode swapPairs(ListNode head) {
	 if(head==null)return head;
	 ListNode value=new ListNode(0);
	 value.next=head;
	 ListNode team=value;
	 ListNode first;
	 ListNode second;
	 while (team!=null&&team.next!=null) {
		if(team.next.next==null)
			return value.next;
		first=team.next;
	    second=team.next.next;
		
	    team.next=second;
	    first.next=second.next;
	    second.next=first;
	    
		team=first;//team=team.next.next
	}
	 return value.next;
 }

在这里插入图片描述

结语

原创不易,bigsai请你帮两件事帮忙一下:

  1. star支持一下, 您的肯定是我在平台创作的源源动力。

  2. 微信搜索「bigsai」,关注我的公众号,不仅免费送你电子书,我还会第一时间在公众号分享知识技术。加我还可拉你进力扣打卡群一起打卡LeetCode。

记得关注、咱们下次再见!

image-20201114211553660

Java
1
https://gitee.com/sillycoder/bigsai-algorithm.git
git@gitee.com:sillycoder/bigsai-algorithm.git
sillycoder
bigsai-algorithm
bigsai-algorithm
master

搜索帮助