13. 判断一个链表是否是回文结构
第一反应是用双指针,但是避免不开要反转链表,没想清楚还是先用最简单的做法:遍历一遍将数据都存到数组中,再前后判断即可。时间复杂度O(N),空间复杂度O(n).
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
ListNode tail = head;
ArrayList<Integer> array = new ArrayList<Integer>();
while(tail!=null){
array.add(tail.val);
tail = tail.next;
}
int j = array.size()-1;
for(int i = 0; i<=j; i++,j--){
if(!array.get(i).equals(array.get(j))) return false;
}
return true;
}
}
一个大坑是需要用equals方法判断是否相等,直接用==在遇到负数的时候会判断错误!!!!!
我知道肯定会有双指针的做法让空间复杂度降到O(1),但是一时间没想出来,还是去看了题解。做法是:用快慢指针,当快指针到结尾时,慢指针刚好到中点,然后慢指针往后的一半的部分反转链表,,和从头开始的指针一起比较这两个半链。
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
//快慢指针找到链表中点
ListNode fast = head;
ListNode slow = fast;
while(fast!=null&&fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
//开始反转链表
ListNode pre = null;
while(slow!=null){
ListNode tmp = slow.next;
slow.next = pre;
pre = slow;
slow = tmp;
}
fast = head;//pre从尾开始遍历,fast从头开始遍历
while(pre!=null){
if(pre.val!=fast.val) return false;
pre = pre.next;
fast = fast.next;
}
return true;
}
}
14.链表奇偶重排
用两个数组分别存奇数位和偶数位,这样非常简单,我特意看了一下要求时间复杂度和空间复杂度要求都是O(N),刚好满足。
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode oddEvenList (ListNode head) {
// write code here
ArrayList<Integer> ji = new ArrayList<Integer>();
ArrayList<Integer> ou = new ArrayList<Integer>();
ListNode cur = head;
for(int i = 1; cur!=null;i++,cur = cur.next){
if(i%2==0) ou.add(cur.val);
else ji.add(cur.val);
}
ListNode res = new ListNode(-1);
cur = res;
for(int i = 0; i < ji.size(); i++){
ListNode tmp = new ListNode(ji.get(i));
res.next = tmp;
res = res.next;
}
for(int i = 0; i < ou.size(); i++){
ListNode tmp = new ListNode(ou.get(i));
res.next = tmp;
res = res.next;
}
return cur.next;
}
}
所以有什么双指针的方法可以让空间复杂度降到O(1)吗?再想了一下,只要用两个指针不断交替两个链表的指向,形成两个链表,最后再用奇链表的尾指向偶链表的头即可,这样空间复杂度降到O(1)
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode oddEvenList (ListNode head) {
// write code here
ListNode ji = head;
ListNode ji_cur = ji;
if(head == null || head.next == null) return head;
ListNode ou = head.next;
ListNode ou_cur = ou;
//为了跳出循环时ji_cur不能指向null
while(ou_cur.next!=null&&ji_cur.next!=null){
ji_cur.next = ou_cur.next;
ji_cur = ji_cur.next;
if(ji_cur.next!=null){
ou_cur.next = ji_cur.next;
ou_cur = ou_cur.next;
}
}
//链表是奇数个节点的时候需要把偶链表末尾改成null
if(ou_cur.next!=null) ou_cur.next = null;
ji_cur.next = ou;
return ji;
}
}