[LeetCode]Swap Nodes in Pairs
发布日期:2021-11-22 02:48:55 浏览次数:2 分类:技术文章

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

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.

题解:

code:

public ListNode swapPairs(ListNode head) {	        		 ListNode p = new ListNode(0);		 p.next = head;		 ListNode tmp = p,curr = p;		 while(curr.next!=null && curr.next.next!=null){			 tmp = curr.next;			 curr.next = tmp.next;			 tmp.next = curr.next.next;			 curr.next.next = tmp;			 curr = tmp;		 }		 return p.next;	 }

转载地址:https://blog.csdn.net/zxdfc/article/details/48713825 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:[LeetCode]Permutations
下一篇:[LeetCode]Generate Parentheses

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月10日 20时04分03秒