203.移除链表元素
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
//在节点ListNode定义中,定义为节点为结构变量。
· 节点存储了两个变量:value 和 next。value 是这个节点的值,next 是指向下一节点的指针,当 next 为空指针时,这个节点是链表的最后一个节点。
· 注意val只代表当前指针的值,比如p->val表示p指针的指向的值;而p->next表示链表下一个节点,也是一个指针。
· 构造函数包含两个参数 _value 和 _next ,分别用来给节点赋值和指定下一节点.
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode ans=new ListNode(-1);//虚拟头结点
ans.next=head;
ListNode cur=ans;
while(cur.next!=null){
if(cur.next.val==val){
cur.next=cur.next.next;
}else{cur=cur.next;}
}
return ans.next;
}
}
707.设计链表
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val=val;
this.next=null;
}
ListNode(int val,ListNode next){
this.val=val;
this.next=next;
}
}
class MyLinkedList {
ListNode head;//带有虚拟头结点
int size;//长度
public MyLinkedList() {//构造函数
this.size=0;
this.head=new ListNode(0);
}
public int get(int index) {
if(index>=size||index<0){
return -1;
}
ListNode cur=head;
for (int i = 0; i <= index; i++) {
cur=cur.next;
}
return cur.val;
}
public void addAtHead(int val) {//头插
addAtIndex(0,val);
}
public void addAtTail(int val) {//尾插
addAtIndex(size,val);
}
public void addAtIndex(int index, int val) {//在index之前添加val结点
if(index>size){
return;
}
ListNode temp=new ListNode(val);
if(index<0){
index=0;
}
ListNode cur=head;
for (int i = 0; i < index; i++) {
cur=cur.next;
}
temp.next=cur.next;
cur.next=temp;
size++;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
if (index == 0) {
head = head.next;
size--;
return;
}
ListNode cur = head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
cur.next = cur.next.next;
size--;
}
public void linktoString(){
ListNode cur=head;
for (int i = 0; i < size-1; i++) {
System.out.print(cur.val+" ");
cur=cur.next;
}
}
}
206.反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
class Solution {
/**
* 反转单链表
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
ListNode pre=null;//虚拟头结点
ListNode cur=head;//遍历结点
ListNode temp=null;
while(cur!=null){
temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
}
}
24.两两交换链表中的结点
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
解题思路:断开链接前要保存好结点的next
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode pre=new ListNode(0);
pre.next=head;
ListNode cur=pre;
while(cur.next!=null && cur.next.next!=null)
{
ListNode temp =cur.next;
cur.next=cur.next.next;
ListNode temp2 =cur.next.next;
cur.next.next=temp;
temp.next=temp2;
cur=cur.next.next;
}
return pre.next;
}
}
19.删除链表的倒数第n个结点
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
解题思路:快指针先走n步,然后慢指针和快指针同时移动,最后快指针暂停的位置,慢指针便是要移除的数。
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre=new ListNode(0);
pre.next=head;
ListNode slow=pre,fast=pre;
for(int i=0;i<n;i++){
fast=fast.next;
}//快指针先走到第n个结点
while(fast.next!=null){//当fast指针不在最后一个结点时
slow=slow.next;
fast=fast.next;
}//因为快指针先走了n个结点,所以当fast经过这次循环后到最后一个结点时,slow刚好走到第n-1个结点
slow.next=slow.next.next;//进行删除结点操作,只用改变slow的next指向即可
return pre.next;
}
}
142.环形链表
public class Solution {
/**
* 判断链表是否有环----->利用快慢指针去找第一个成环的结点
* @param head
* @return
*/
public ListNode detectCycle(ListNode head) {
ListNode fast=head,slow=head;//快指针一次走两步,慢指针一次走一步
if(head==null||head.next == null){
return null;
}
//如果没环的话fast会走到头
while (fast!=null&&fast.next!=null&&slow!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
fast=head;
while (fast!=slow){
fast=fast.next;
slow=slow.next;
}
return fast;
}
}
return null;
}
}
面试题02.07.链表相交
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
class Solution {
/**
* 判断两个链表是否相交---->返回相交的第一个结点
*
* @param headA
* @param headB
* @return
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode huanA = detectCycle(headA);
ListNode huanB = detectCycle(headB);//判断AB是否有环
//情况1:AB都没有环
if (huanA == null && huanB == null) {
ListNode endA = searchLastNode(headA);
ListNode endB = searchLastNode(headB);//找AB最后的结点
if (endA != endB) {
//两链表不可能相交
return null;
}
//两链表相交,且AB无环---->找链表的差值,长的那个先走差值步,然后每个都开始走一步----->直到相交
return sectionA(headA, headB);
}
//都有环
else if (huanA != null && huanB != null) {
if (huanA == huanB) {//出现环的第一个结点相等--->看作A情况
return sectionA(headA, headB);
} else {//出现环的第一个结点不相交------->可能能绕出来环,也有可能绕不出来
ListNode cur = huanA;
while (true) {
cur = cur.next;
if (cur == huanB) {//可以绕道环B上
return huanA;//此时return环A和环B都可以
}
if (cur == huanA) {//绕回环A
return null;
}
}
}
}
return null;//一个有环一个没环,不可能相交
}
/**
* 判断链表是否有环----->利用快慢指针去找第一个成环的结点
*
* @param head
* @return
*/
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;//快指针一次走两步,慢指针一次走一步
if (head == null || head.next == null) {
return null;
}
//如果没环的话fast会走到头
while (fast != null && fast.next != null && slow != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
/**
* 找这个链表最后一个结点
*
* @param head
* @return
*/
public ListNode searchLastNode(ListNode head) {
ListNode cur = head;
while (cur.next != null) {
cur = cur.next;
}
return cur;
}
/**
* 找这个链表的长度
*
* @param head
* @return
*/
public int listLength(ListNode head) {
int sum = 0;
ListNode cur = head;
while (cur != null) {
sum++;
cur = cur.next;
}
return sum;
}
/**
* 找链表的差值,长的那个先走差值步,然后每个都开始走一步----->直到相交
*
* @return 第一个相交的结点
*/
public ListNode sectionA(ListNode headA, ListNode headB) {
int sumA = listLength(headA);
int sumB = listLength(headB);
ListNode fast = new ListNode(0);
ListNode slow = new ListNode(0);
if (sumA >= sumB) {
fast = headA;
slow = headB;
for (int i = 0; i < sumA - sumB; i++) {
fast = fast.next;
}
} else {//B的长度比较长
fast = headB;
slow = headA;
for (int i = 0; i < sumB - sumA; i++) {
fast = fast.next;
}
}//先走差值步--->然后一起走
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}