24.两两交换链表中的节点
思路
一开始暴力解,直接把两个节点的值做了交换。但是题目是要整个节点都换掉。
错误代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null || head.next==null) return head;
swapPairs(head.next.next);
int temp=head.val;
head.val=head.next.val;
head.next.val=temp;
return head;
}
}
但是照着两两一组的思路,做链表反转,然后再把断开的地方连接起来就可以了。
需要注意的点是递归返回的是新的头节点,一开始我返回了原来的头节点(即反转后的尾节点)导致出错
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null || head.next==null) return head;
ListNode newtail=swapPairs(head.next.next);
ListNode newhead=head.next;
newhead.next=head;
head.next=newtail;
return newhead;
}
}
19.删除链表的倒数第N个节点
思路
一开始想的是翻转链表,正向删除节点,删除后再翻转回来,但是需要考虑的边界值比较麻烦。处理链表双指针/递归,所以第二次就试着用双指针来解(涉及到增删改考虑虚拟节点)
代码
递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
int count=0;
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return head;
ListNode newhead=reverse(head);
if(n==1) return reverse(newhead.next);
ListNode pre=newhead;
int count=2;
while(count<n){
pre=pre.next;
count++;
}
pre.next=pre.next.next;
ListNode p=reverse(newhead);
return p;
}
public ListNode reverse(ListNode head){
if(head==null || head.next==null) return head;
ListNode tail=reverse(head.next);
head.next.next=head;
head.next=null;
return tail;
}
}
双指针
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
int count=0;
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy=new ListNode(-1);
dummy.next=head;
ListNode fast=dummy;
ListNode slow=dummy;
for(int i=0;i<=n;i++){
fast=fast.next;
}
while(fast!=null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return dummy.next;
}
}
面试题 02.07. 链表相交
思路
方向都是拼接链表,一直往下走直到指针相交。但是自己写的时候出现了一个错误,就是我以为直接改变指针指向会把两个链表拼在一起(因为题目说了不能改变原始结构),但其实没有做.next指向是没有问题的。
代码
一开始写的
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode ANode=headA;
ListNode BNode=headB;
ListNode ANodeB=headB;
ListNode BNodeA=headA;
while(ANode!=null && BNode!=null){
ANode=ANode.next;
BNode=BNode.next;
}
if(ANode==null){
while(BNode!=null){
ANodeB=ANodeB.next;
BNode=BNode.next;
}
while(ANodeB!=BNodeA){
ANodeB=ANodeB.next;
BNodeA=BNodeA.next;
}
}else{
while(ANode!=null){
BNodeA=BNodeA.next;
ANode=ANode.next;
}
while(ANodeB!=BNodeA){
ANodeB=ANodeB.next;
BNodeA=BNodeA.next;
}
}
return BNodeA;
}
}
实际上可以优化为
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode ANode=headA;
ListNode BNode=headB;
while(ANode!=BNode){
if(ANode!=null) ANode=ANode.next;
else ANode=headB;
if(BNode!=null) BNode=BNode.next;
else BNode=headA;
}
return BNode;
}
}
142.环形链表II
思路
用双指针,快的那个一步步追赶慢的。快慢指针的交点和从头出发的指针一步步往前,遇到的点就是环的起点。(fast走的是slow的两倍)
代码
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
ListNode meet=head;
while(meet!=fast){
meet=meet.next;
fast=fast.next;
}
return meet;
}
}
return null;
}
}