1、删除链表的倒数第N个节点
2、链表相交
3、字符串中第二大的数字
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

解题思路–快慢指针:
关键在于怎么找倒数第n个节点:快指针先走n+1步,快慢指针同时出发,当快指针=null时,慢指针此时所在位置就是要删除节点的前驱


/*** 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 dummy = new ListNode(-1,head);ListNode fast=dummy;ListNode slow=dummy;int count = 0;while(count++//快指针先走n+1步fast = fast.next;}while(fast!=null){fast=fast.next;slow=slow.next;}slow.next=slow.next.next;return dummy.next;}
}

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

解题思路:


/*** 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 curA = headA;ListNode curB = headB;//求链表A的长度int lengthA=0;while(curA!=null){lengthA++;curA=curA.next;}//求链表B的长度int lengthB=0;while(curB!=null){lengthB++;curB=curB.next;}curA = headA;curB = headB;//让curA为最长链表的头 if(lengthAint tempLength = lengthA;lengthA = lengthB;lengthB = tempLength;ListNode tempNode = curA;curA = curB;curB = tempNode;}int step = lengthA-lengthB;//链表A的指针先往前走step+1个位置for(int i=0;icurA=curA.next;}while(curA!=null){if(curA==curB){return curA;}curA = curA.next;curB = curB.next;}return null;}
}

给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。
混合字符串 由小写英文字母和数字组成。

class Solution {public int secondHighest(String s) {char[] chars = s.toCharArray();int max1=-1;int max2=-1;for(char c : chars){if(Character.isLetter(c))continue;int num = c-'0';if(num>max1){max2=max1;max1=num;}else if(nummax2){max2=num;}}return max2;}
}
