1链表
1合并两个有序链表21 (简单)
思路:建立一个新节点,依次将两个链表中较小的节点接到该节点上,指针移动,最后将较长链表剩余的元素拼接到链表尾部
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head=new ListNode();
ListNode temp=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
head.next=l1;
l1=l1.next;
}
else{
head.next=l2;
l2=l2.next;
}
head=head.next;
}
if(l1!=null){
head.next=l1;
}
if(l2!=null){
head.next=l2;
}
return temp.next;
}
}
2合并k个有序链表23 (困难)
思路与合并两个有序链表基本一致,都是找到最小值拼接到链表。
使用优先队列来维护保存最小值的序列
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists==null||lists.length==0)return null;
PriorityQueue<ListNode> pq = new PriorityQueue<>(
lists.length, (a, b)->(a.val - b.val));
//将每个链表的最小元素加入到优先队列
for(int i=0;i<lists.length;i++){
if(lists[i]!=null)
pq.add(lists[i]);
}
ListNode head=new ListNode();
ListNode temp=head;
//将优先队列中最小值拼接到链表
while(!pq.isEmpty()){
ListNode node=pq.poll();
head.next=node;
//如果链表还有节点,将节点加入优先队列,更新最小值
if(node.next!=null){
pq.add(node.next);
}
head=head.next;
}
return temp.next;
}
}
3删除倒数第n个节点19(中等)
思路:先走k步,用一指针指向链表头,再走n-k步到链表尾部,就可以得到第n-k个节点
使用虚拟头节点,防止边界情况的出现
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//需要一个虚拟头节点,防止出现删除第一个或者最后一个节点的情况
ListNode temp=new ListNode();
temp.next=head;
//找到倒数第n+1个节点
ListNode node=find(temp,n+1);
node.next=node.next.next;
return temp.next;
}
public ListNode find(ListNode head, int n){
ListNode node1=head;
ListNode node2=head;
//先走n步
while(n>0){
node1=node1.next;
n--;
}
//走到链表尾部。即走n-k步
//定义一个指针从头开始,那么循环结束,该指针走了n-k步,得到答案
while(node1!=null){
node1=node1.next;
node2=node2.next;
}
return node2;
}
}
4链表的中点876 (简单)
class Solution {
public ListNode middleNode(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
}
5链表是否有环141 (简单)
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow)return true;
}
return false;
}
}
6链表的环的起点142 (中等)
思路:先找到快慢指针相等的节点,使其中一个指针指向头节点,以相同的速度移动即可得到环的起点
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(slow==fast)break;
}
if(fast==null||fast.next==null)return null;
slow=head;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return fast;
}
}
7相交链表160 (简单)
思路:链表遍历完成后指向另外一个链表,由链表长度不一样的性质,可以达到快慢指针的作用,使得两个指针通知到达交点
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode node1=headA;
ListNode node2=headB;
ListNode temp1=headA;
ListNode temp2=headB;
while(temp1!=temp2){
temp1=temp1==null?headB:temp1.next;
temp2=temp2==null?headA:temp2.next;
}
return temp1;
}
8反转链表206 (简单)
思路:递归思想
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode node=reverseList(head.next);
head.next.next=head;
head.next=null;
return node;
}
}
9反转链表的一部分92 (中等)
思路:通过递归和数学相对关系,把反转链表的一部分转化为反转前n个节点,把反转前n个节点转化为反转整个链表
public ListNode reverseBetween(ListNode head, int left, int right) {
if(left==1){
return reverseN(head,right);
}
head.next=reverseBetween(head.next,left-1,right-1);
return head;
}
ListNode temp;
public ListNode reverseN(ListNode head,int n){
if(n==1){
temp=head.next;
return head;
}
ListNode last=reverseN(head.next,n-1);
head.next.next=head;
head.next=temp;
return last;
}
10k个一组反转链表25 (困难)
思路:先反转前k个元素,然后从反转后的位置开始递归,又是反转前k个元素,也就是分组反转转化为反转前n个元素
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode a=head,b=head;
//判断是否有k个元素,不到k个元素则不变
for(int i=0;i<k;i++){
if(b==null) return head;
b=b.next;
}
ListNode newHead=reverse(a,b);
a.next=reverseKGroup(b,k);
return newHead;
}
//反转[a,b)
public ListNode reverse(ListNode a,ListNode b){
ListNode pre,cur,next;
pre=null;
cur=a;
next=a;
while(cur!=b){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
//返回反转后的头节点
return pre;
}
}
11判断回文链表234 (简单)
思路:链表的后序遍历即可实现从尾部向前遍历
class Solution {
ListNode left;
public boolean isPalindrome(ListNode head) {
//方法一 链表的后序遍历即可实现从尾部向前遍历
left=head;
return comp(head);
}
public boolean comp(ListNode right){
if(right==null) return true;
boolean res=comp(right.next);
res=res&&(left.val==right.val);
left=left.next;
return res;
}
}
思路:快慢指针找到链表中点,反转一半链表,再进行比较
class Solution {
public boolean isPalindrome(ListNode head) {
//方法2 快慢指针找到链表中点,反转一半链表,再进行比较
ListNode slow=head,fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
if(fast!=null){
slow=slow.next;
}
ListNode left=head;
ListNode right=reverse(slow);
while(right!=null){
if(left.val!=right.val)return false;
left=left.next;
right=right.next;
}
return true;
}
public ListNode reverse(ListNode head){
ListNode pre=null,cur=head,next;
while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
}