Swap Nodes in Pairs
2015-10-12 23:18
525 查看
Swap Nodes in Pairs
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.
给定一个链表,把相邻两个结点调换位置;返回head
Java代码:
输出:
2 1 4 3 6 5 8 7 9
这个方法是先处理前2个结点,再循环处理后续的结点。其实结点的处理方法都差不多,在LeetCode讨论区看到递归解法,搬运过来
Java代码:
利用方法开头对head是否为null的判断作为递归的条件,比第一个方法优雅很多
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.
给定一个链表,把相邻两个结点调换位置;返回head
Java代码:
package com.rust.cal; /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class SwapNodesinPairs { public static ListNode swapPairs(ListNode head) { if (head == null || head.next == null) { //必须先判断head是否为null,否则会出java.lang.NullPointerException //如果输入的head == null,先判断head.next会找不到目标 return head; } /* 针对前两个结点 */ ListNode pre = head.next, later, veryFirst; head.next = pre.next; pre.next = head; head = pre; later = head.next; /* * 针对后续结点 * 连续有2个结点,才进行换位 */ while (later.next != null && later.next.next != null) { veryFirst = later; pre = pre.next.next; later = later.next.next; pre.next = later.next; later.next = pre; veryFirst.next = later; later = pre; pre = veryFirst.next; } return head; } public static void main(String args[]){ /* * prepare data */ ListNode head = new ListNode(1); ListNode initHead = head; for (int i = 2; i < 10; i++) { initHead.next = new ListNode(i); initHead = initHead.next; } head = swapPairs(head); /* * show data */ ListNode newHead = head; while(newHead != null){ System.out.print(newHead.val + " "); newHead = newHead.next; } ListNode nothing = new ListNode(1); swapPairs(nothing.next); } }
输出:
2 1 4 3 6 5 8 7 9
这个方法是先处理前2个结点,再循环处理后续的结点。其实结点的处理方法都差不多,在LeetCode讨论区看到递归解法,搬运过来
Java代码:
public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) return head; ListNode n1 = head; ListNode n2 = head.next; n1.next = n2.next; n2.next = n1; n1.next = swapPairs(n1.next); return n2; }
利用方法开头对head是否为null的判断作为递归的条件,比第一个方法优雅很多
相关文章推荐
- nodejs API之QueryString
- nodejs API之URL
- [LeetCode]题解(python):025-Reverse Nodes in k-Group
- [LeetCode]题解(python):024-Swap Nodes in Pairs
- Leetcode92: Swap Nodes in Pairs
- leetcode Populating Next Right Pointers in Each Node
- lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点
- node.js安装和使用步骤-windows
- Remove Nth Node From End of List
- 自制工具:迅速打开一个Node 环境的Playground
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- nodejs入门实践
- js下firstElementChild firstChild 以及childNodes和children方法
- Nodejs入门学习——fs.renameSync出错
- ReactPHP── PHP版的Node.js
- nodejs更改文件夹下的文件名
- NodeJs 中的Crypto 加密模块
- android GMS认证之testNoDebuggable
- node.js (1)安装与环境配置
- nodeclub route