关于LeetCode中Swap Nodes in Pairs一题的理解
2016-09-05 19:46
453 查看
题目如下:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
题目也是非常容易理解,给定一个链表,交换“所有”两个毗邻的节点,然后返回表头。为了更好地说明题干的意思,咱们还是来看一下题干中给的例子。给定一个链表1->2->3->4,交换之后变成了2->1->4->3也就是说,他是一对一对进行交换的,是先将所有链表分好堆,然后交换堆里的两个节点,并不存在一个堆中的元素和另一个堆中的元素进行交换的情况。就是说,上述链表第一次交换进行完之后变为2->1->3->4,下一次交换直接在3->4进行,不是在1->3进行。
好了,明确了题干的意思之后,我们就要开始进行分析了。我们可以把新链表的形成大致分解成两个过程,首先是链表开始处“堆”中的两个元素进行交换,然后是交换后的位置在后面的元素应该指向下一个“正确的”元素,什么是“正确的”元素呢?就是当前“堆”紧挨着的下一个“堆”,我们只要将这个“堆”中的元素进行交换,然后将交换后位置较前的元素返回给上一个“堆”中位置靠后的元素的next即可。这个过程以此类推,直到链表最后位置,值依次向前返回,是一个自底向上的过程,显然需要用“递归”的方式来解决,编码时注意边界条件及开始节点为null的情况的判定即可。老规矩,上已Accepted的代码:
大致就是这样了,今天搬到了新工位,有新电脑用,这个feel不得不说还是棒儿爽的,哈哈!
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.
题目也是非常容易理解,给定一个链表,交换“所有”两个毗邻的节点,然后返回表头。为了更好地说明题干的意思,咱们还是来看一下题干中给的例子。给定一个链表1->2->3->4,交换之后变成了2->1->4->3也就是说,他是一对一对进行交换的,是先将所有链表分好堆,然后交换堆里的两个节点,并不存在一个堆中的元素和另一个堆中的元素进行交换的情况。就是说,上述链表第一次交换进行完之后变为2->1->3->4,下一次交换直接在3->4进行,不是在1->3进行。
好了,明确了题干的意思之后,我们就要开始进行分析了。我们可以把新链表的形成大致分解成两个过程,首先是链表开始处“堆”中的两个元素进行交换,然后是交换后的位置在后面的元素应该指向下一个“正确的”元素,什么是“正确的”元素呢?就是当前“堆”紧挨着的下一个“堆”,我们只要将这个“堆”中的元素进行交换,然后将交换后位置较前的元素返回给上一个“堆”中位置靠后的元素的next即可。这个过程以此类推,直到链表最后位置,值依次向前返回,是一个自底向上的过程,显然需要用“递归”的方式来解决,编码时注意边界条件及开始节点为null的情况的判定即可。老规矩,上已Accepted的代码:
public ListNode swapPairs(ListNode head) { if(head==null){ return null; } ListNode nxt = head.next; if(head.next!=null){ head.next = nxt.next; nxt.next = head; head.next = swapPairs(head.next); }else{ return head; } return nxt; }然后是评论区的代码,推荐这个无递归版,我就喜欢这种不用递归就要倒节点的程序员,代码如下:
public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null && current.next.next != null) { ListNode first = current.next; ListNode second = current.next.next; first.next = second.next; current.next = second; current.next.next = first; current = current.next.next; } return dummy.next; }上面的代码大家应该都能看懂,我就不过多进行解释了,但是这种方法真的很重要!如果实在不明白的话就自己用这种方式跑一遍链表1->2->3->4->5,肯定能懂。然后还有个哥们在这个回答的reply区贴了一个简单的过程分析,可以去看一看,地址在这里,reply的那个人叫ravi2:https://discuss.leetcode.com/topic/10649/my-simple-java-solution-for-share/4
大致就是这样了,今天搬到了新工位,有新电脑用,这个feel不得不说还是棒儿爽的,哈哈!
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解