作者:✿✿ xxxflower. ✿✿
博客主页:xxxflower的博客
专栏:【力扣/牛客刷题】篇
语录:⭐每一个不曾起舞的日子,都是对生命的辜负。⭐
题目OJ链接:203. 移除链表元素

【题目分析】我们先来分析一下我们需要注意什么
我们先设置两个值:



/**
* 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 removeElements(ListNode head, int val) {
//1. 链表为空
if(head == null){
return null;
}
//2. 所要删除的数字位于链表中间(即最普通的情况)
ListNode cur = head.next;
ListNode prev = head;
while(cur != null){
if(cur.val == val){
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
//3. 链表中要寻找的数字从头开始出现多次(例如:23 23 23 45 23)(删除23)此时以上代码执行完时,还有第一个没有删除,因此需要在判断一下
if(head.val == val){
head = head.next;
}
return head;
}
}

题目OJ链接: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) {
//1.没有节点
if(head == null){
return null;
}
//2.只有一个节点
if(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;
}
}

题目OJ链接:876.链表的中间结点

【题目分析】本题可以利用快慢指针来完成。

/**
* 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 middleNode(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while((fast != null)&&(fast.next != null)){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}

/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(k < 0 ||head == null){
return null;
}
ListNode fast = head;
ListNode slow = head;
while(k-1 != 0){
fast = fast.next;
if(fast == null){
return null;
}
k--;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
21. 合并两个有序链表


思路:定义一个虚拟头结点,比较两个链表头结点的值的大小。

/**
* 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 mergeTwoLists(ListNode head1,
ListNode head2) {
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while(head1 != null && head2 != null){
if(head1.val < head2.val){
tmp.next = head1;
head1 = head1.next;
}else{
tmp.next = head2;
head2 = head2.next;
}
tmp = tmp.next;
}
if(head1 != null){
tmp.next = head1;
}
if(head2 != null){
tmp.next = head2;
}
return newHead.next;
}
}
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
ListNode as=null;
ListNode ae=null;
ListNode bs=null;
ListNode be=null;
ListNode cur=pHead;
while(cur!=null){
if(cur.val>=x){
if(as==null){
as=cur;
ae=cur;
}else{
ae.next=cur;
ae=ae.next;
}
}else{
if(bs==null){
bs=cur;
be=cur;
}else{
be.next=cur;
be=be.next;
}
}
cur=cur.next;
}
if(bs==null){
return as;
}
be.next=as;
if(as!=null){
ae.next=null;
}
return bs;
}
}
题目Oj:OR36 链表的回文结构



import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class PalindromeList {
public boolean chkPalindrome(ListNode head) {
// write code here
if(head == null){
return false;
}
if(head.next == null){
return true;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
ListNode cur = slow.next;;
while(cur != null){
ListNode curNext = cur.next;
cur.next = slow;
slow = cur;
cur = curNext;
}
while(head != slow){
if(head.val != slow.val){
return false;
}
if(head.next == slow){
return true;
}
head = head.next;
slow = slow.next;
}
return true;
}
}

/**
* 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) {
// 1.计算A和B链表的长度
int headAlen=headLen(headA);
int headBlen=headLen(headB);
int n=0;
// 2.求其差值并让长的链表走差值步
if(headAlen>headBlen){
n=headAlen-headBlen;
}else{
n=headBlen-headAlen;
}
if(headAlen>headBlen){
while(n!=0){
headA=headA.next;
n--;
}
}else{
while(n!=0){
headB=headB.next;
n--;
}
}
// 3.两个链表同时走,如果相等则返回即为相遇值
while(headA!=null && headB!=null){
if(headA==headB){
return headA;
}
headA=headA.next;
headB=headB.next;
}
return null;
}
// 计算链表长度的函数
public int headLen(ListNode head){
int count=0;
if(head==null){
return 0;
}
while(head!=null){
count++;
head=head.next;
}
return count;
}
}
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null){
return false;
}
ListNode fast=head.next;
ListNode slow=head;
// 注意此处的条件是快指针不为空
while(fast!=null &&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
return true;
}
}
return false;
}
}
