移除链表元素
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while(pre != null && pre.next != null){
if(pre.next.val == val){
pre.next = pre.next.next;
}else{
pre = pre.next;
}
}
return dummy.next;
}
}
1. 链表的中间结点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
// 快慢指针:
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
//普通方法:
class Solution {
public ListNode middleNode(ListNode head) {
int len = 0;
ListNode cur = head;
while(cur != null){
cur = cur.next;
len++;
}
int len2 = len/2;
cur = head;
for(int i = 0; i < len2; i++){
cur = cur.next;
}
return cur;
}
}
2. 链表中倒数第K个节点
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode slow = head;
ListNode fast = head;
int i = 0;
while(fast != null){
fast = fast.next;
if(i >= k){
slow = slow.next;
}
i++;
}
if(i < k){
return null;
}
return slow;
}
}
3. 链表分割
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) {
if(pHead == null && pHead.next == null){
return pHead;
}
ListNode bStart = new ListNode(-1);
ListNode bEnd = bStart;
ListNode aStart = new ListNode(-1);
ListNode aEnd = aStart;
ListNode cur = pHead;
while(cur != null){
if(cur.val < x){
bEnd.next = new ListNode(cur.val);
bEnd = bEnd.next;
}else{
aEnd.next = new ListNode(cur.val);
aEnd = aEnd.next;
}
cur = cur.next;
}
bEnd.next = aStart.next;
return bStart.next;
}
}
4. 翻转链表(迭代与递归两种方法)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
return reverse(null,head);
}
public static ListNode reverse(ListNode prev,ListNode cur) {
if(cur == null){
return prev;
}
ListNode curNext = cur.next;
cur.next = prev;
return reverse(cur,curNext);
}
}
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null){
ListNode curNext = cur.next;
cur.next = pre;
pre = cur;
cur = curNext;
}
return pre;
}
}
*** 5. 删除链表中的相同元素
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead){
if(pHead == null || pHead.next == null){
return pHead;
}
ListNode dummyHead = new ListNode(-1);
ListNode tempHead = dummyHead;
ListNode cur = pHead;
while(cur != null){
if(cur.next != null && cur.val == cur.next.val){
while (cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
cur = cur.next;
dummyHead.next = cur;
}else{
dummyHead.next = cur;
dummyHead = cur;
cur = cur.next;
}
}
return tempHead.next;
}
}
6. 链表的回文结构
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
//如果链表为空,直接反回true,如果链表只有一个节点,直接返回true
if (A == null){
return false;
}else if (A.next == null){
return true;
}
ListNode slow = A;
ListNode fast = A;
while (fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode p = slow.next;
ListNode p1 = p.next;
//将后半部分的链表逆置
while (p != null){
p.next = slow;
slow = p;
p = p1;
if (p1 != null) {
p1 = p1.next;
}
}
while(slow != A) {
if (slow.val != A.val){
return false;
}
//偶数节点
if (A.next == slow){
return true;
}
A = A.next;
slow = slow.next;
}
return true;
}
}
7. 判断链表是否有环
/**
* 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) {
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
return true;
}
}
return false;
}
}
8. 返回环起始位置
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
}
9.相交链表
/**
* 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) {
if (headA == null || headB == null) {
return null;
}
int alen = getLength(headA);
int blen = getLength(headB);
if (alen < blen) {
for (int i = 0; i < blen - alen; i++) {
headB = headB.next;
}
} else {
for (int i = 0; i < alen - blen; i++) {
headA = headA.next;
}
}
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headB;
}
public static int getLength(ListNode tmp) {
int cnt = 0;
while (tmp != null) {
cnt++;
tmp = tmp.next;
}
return cnt;
}
}
10.合并两个有序链表
/**
* 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) {
if (headA == null || headB == null) {
return null;
}
int alen = getLength(headA);
int blen = getLength(headB);
if (alen < blen) {
for (int i = 0; i < blen - alen; i++) {
headB = headB.next;
}
} else {
for (int i = 0; i < alen - blen; i++) {
headA = headA.next;
}
}
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headB;
}
public static int getLength(ListNode tmp) {
int cnt = 0;
while (tmp != null) {
cnt++;
tmp = tmp.next;
}
return cnt;
}
}