提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
一、力扣707. 设计链表
class MyLinkedList {
private Node head;
private int count;
class Node {
int val;
Node next;
public Node(int val, Node next){
this.val = val;
this.next = next;
}
public Node(){
}
}
public MyLinkedList() {
this.head = new Node(-1,null);
this.count = 0;
}
public int get(int index) {
if(index >= count){
return -1;
}
Node p = head;
for(int i = 0; i <= index; i ++){
p = p.next;
}
return p.val;
}
public void addAtHead(int val) {
addAtIndex(0,val);
}
public void addAtTail(int val) {
addAtIndex(count,val);
}
public void addAtIndex(int index, int val) {
if(index > count){
return;
}
Node pre = this.head;
Node p = new Node(val,null);
while(index > 0){
index --;
pre = pre.next;
}
p.next = pre.next;
pre.next = p;
count ++;
}
public void deleteAtIndex(int index) {
if(index >= count){
return;
}
Node pre = head;
for(int i = 0; i < index; i ++){
pre = pre.next;
}
pre.next = pre.next.next;
count --;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
二、力扣206. 反转链表
/**
* 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 reverseList(ListNode head) {
if(head == null){
return head;
}
ListNode L = new ListNode(-1,null);
ListNode p = head;
while(p != null){
ListNode s = p;
p = p.next;
s.next = L.next;
L.next = s;
}
return L.next;
}
}
三、力扣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;
}
ListNode p1 = head, p2 = head.next;
ListNode L = new ListNode(-1,head);
ListNode pre = L, s1, s2;
while(p1 != null && p2 != null){
s1 = p1; s2 = p2;
if(p2.next == null){
p1 = null;
p2 = null;
}else if(p2.next != null && p2.next.next == null){
p1 = p2.next;
p2 = null;
}else if(p2.next != null && p2.next.next != null){
p1 = p2.next;
p2 = p1.next;
}
pre.next = s2;
s2.next = s1;
pre = s1;
s1.next = p1;
}
return L.next;
}
}
四 、力扣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 {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode L = new ListNode(-1,head);
ListNode pre = L, p = L;
for(int i = 0; i <= n; i ++){
p = p.next;
}
while(p != null){
p = p.next;
pre = pre.next;
}
pre.next = pre.next.next;
return L.next;
}
}