Swap Nodes & Reverse Nodes in k-Group
Swap Nodes |
Given a linked list, swap every two adjacent nodes and return its head.
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
分析:使用递归,方便又快捷。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @return a ListNode
*/
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head; ListNode prev = head;
ListNode current = head.next;
head = current.next;
current.next = prev; prev.next = swapPairs(head); return current;
}
}
Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory is allowed.
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || !hasKElements(head, k)) return head; ListNode prev = null;
ListNode current = head;
ListNode next = null;
int i = ;
while (i <= k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
i++;
} head.next = reverseKGroup(current, k);
return prev;
} private boolean hasKElements(ListNode head, int k) {
while(k >= ) {
if (head == null) return false;
k--;
head = head.next;
}
return true;
}
}
Swap Nodes & Reverse Nodes in k-Group的更多相关文章
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- [Swift]LeetCode25. k个一组翻转链表 | Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- [leetcode]25. Reverse Nodes in k-Group每k个节点反转一下
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
随机推荐
- java日期处理总结
Java日期时间使用总结 一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式 ...
- [转]Java中的private、protected、public和default的区别
原文地址:http://my.oschina.net/u/1169535/blog/403589 (1)对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类.接口. ...
- Java基础-父类-子类执行顺序
代码解析 子类 package com; /** * 子类 * @author huage * */ public class Test extends Test1{ public static vo ...
- 2.Android之按钮Button和编辑框EditText学习
今天学习android一些基本控件:button和edittext使用,我们平时网页或者手机登录一般都会有登录框,这里面就用到这两个部件. 如图: 所对应XML文件: 一般第二个编辑框都是输入密码,所 ...
- 统计"1"个数问题
问题: 给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数. 例如:N=2时 1,2出现了1个 “1” . N=12时 1,2,3,4,5,6,7,8,9,10,11,12.出现了5个“1 ...
- POJ1011 Sticks
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
- POJ 2182 Lost Cows
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10996 Accepted: 7059 Description N (2 ...
- gotoTop返回顶部 JS
方法: 1.首先在body添加一个标签,在一个页面添加,其它页面也会生效. <body> <a name="top"> 2.然后在页脚添加一个链接 < ...
- php中静态变量和静态方法
1,静态变量:所有对象共享的变量成为静态变量.静态变量类似于全局变量,不过全局变量破坏对象的封装性,因此其对应于面向过程:静态变量对应于面向对象. 2,全局变量,全局变量的使用实例如下,声明全局变量时 ...
- easyui datagrid 获取 title
function exportExecl(obj) { var cfs = $(obj).datagrid('getColumnFields'); //这是获取到所有的Fields //得到title ...