目录
一、顺序表的缺点
二、链表
1、特点
2、存在的几种链式结构
3、LinkedList
(1)底层原理
(2)可操作方法
4、常见面试题
(1)删除链表中等于给定值 val 的所有节点。
(2)反转一个单链表。
(3)返回链表的中间结点
(4)输出该链表中倒数第k个结点
(5)两个有序链表合并为一个新的有序链表并返回
(6)以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
(7)链表的回文结构
(8)输入两个链表,找出它们的第一个公共结点
(9)给定一个链表,判断链表中是否有环
(10)给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL
三、顺序表与链表区别
在顺序表中任意位置插入或删除元素时,需要把元素整体往后或往前移动,时间复杂度为O(n)。因此顺序表不适用于任意位置插入或删除元素的场景。
在逻辑上是连续的,但在物理上不一定是连续的。
(1)单向
(2)双向
(3)无头
(4)有头
(5)循环
(6)不循环
是无头双向循环链表。
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null){
return null;
}
ListNode pre=head;
ListNode cur=pre.next;
while(cur!=null){
if(cur.val==val){
pre.next=cur.next;
cur=cur.next;
}else{
pre=cur;
cur=cur.next;
}
}
if(head.val==val){
head=head.next;
}
return head;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode cur=head.next;
head.next=null;
while(cur!=null){
ListNode curnext=cur.next;
cur.next=head;
head=cur;
cur=curnext;
}
return head;
}
}
class Solution {
public ListNode middleNode(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
}
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null) return null;
ListNode cur=head;
int size=0;
while(cur!=null){
size++;
cur=cur.next;
}
if(k<=0||k>size){
return null;
}
ListNode fast=head;
ListNode slow=head;
while(k-1!=0){
fast=fast.next;
k--;
}
while(fast.next!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(k<=0||head==null){
return null;
}
ListNode fast=head;
ListNode slow=head;
int count=0;
//要么走k-1步结束循环,要么长度不够长退出循环
while(k-1!=count){
fast=fast.next;
if(fast==null){
return null;
}
count++;
}
while(fast.next!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1==null) return list2;
if(list2==null) return list1;
ListNode head1=new ListNode(0);
ListNode head2=head1;
while(list1!=null&&list2!=null){
if(list1.val
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode head1=null;
ListNode cur1=null;
ListNode cur2=null;
ListNode head2=null;
while(pHead!=null){
if(pHead.val
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
// write code here
if(A==null||A.next==null) return true;
ListNode slow=A;
ListNode fast=A;
ListNode B=null;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
if(fast==null){
B=slow;
}else{
B=slow.next;
}
ListNode C=reverseList(B);
while(C!=null){
if(C.val!=A.val){
return false;
}
C=C.next;
A=A.next;
}
return true;
}
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode cur=head.next;
head.next=null;
while(cur!=null){
ListNode curnext=cur.next;
cur.next=head;
head=cur;
cur=curnext;
}
return head;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null&&headB!=null) return null;
if(headA!=null&&headB==null) return null;
ListNode cur1=headA;
ListNode cur2=headB;
int size1=size(cur1);
int size2=size(cur2);
int difference=0;
if(size1>size2){
difference=size1-size2;
while(difference!=0){
cur1=cur1.next;
difference--;
}
}else{
difference=size2-size1;
while(difference!=0){
cur2=cur2.next;
difference--;
}
}
while(cur1!=null){
if(cur1==cur2){
return cur1;
}else{
cur1=cur1.next;
cur2=cur2.next;
}
}
return null;
}
public int size(ListNode cur){
int count=0;
while(cur!=null){
count++;
cur=cur.next;
}
return count;
}
}
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow)
return true;
}
return false;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null||head.next==null){
return null;
}
ListNode cur=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;
ListNode cur1=slow;
//一个从相遇点出发,一个从起点出发,再次相遇时,相遇点为环的起点
while(cur!=cur1){
cur=cur.next;
cur1=cur1.next;
}
return cur;
}
}
nR-X=(n-1)R+R-x; L大,环小,多走几圈。