leetcode24 两两交换链表
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode();
ListNode cur = dummy;
cur.next = head;
/*
dummy -> 1 -> 2 -> 3 -> 4
| | |
cur temp temp1
要操作的数是cur之后的两个数,如果next为空则是偶数个,next的next为空是奇数个
*/
while (cur.next != null || cur.next.next != null) {
ListNode temp = cur.next;
ListNode temp1 = cur.next.next.next;
cur.next = cur.next.next;
cur.next.next = temp;
temp.next = temp1;
cur = cur.next.next;
}
return dummy.next;
}
leetcode206 翻转链表
递归解法
public ListNode reverseList(ListNode head) {
//递归解法
ListNode pre = null;
ListNode cur = head;
return reverse(pre, cur);
}
private ListNode reverse(ListNode pre, ListNode cur) {
if (cur == null) {
return pre;
}
ListNode temp = cur.next;
cur.next = pre;
return reverse(cur, temp);
}
非递归解法
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
leetcode92 翻转链表二
public ListNode reverseBetween(ListNode head, int left, int right) {
if (head == null || left >= right) {
return head;
}
ListNode dummy = new ListNode();
dummy.next = head;
ListNode p0 = dummy;
//寻找规定的首结点
for (int i = 0; i < left - 1; i++) {
p0 = p0.next;
}
ListNode cur = p0.next;
ListNode pre = null;
for (int i = 0; i < right - left + 1; i++) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
p0.next.next = cur;
p0.next = pre;
return dummy.next;
}
leetcode83 删除排序链表中的重复元素
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return head;
}
ListNode cur = head;
while (cur.next != null) {
if (cur.val == cur.next.val) {
//删除下一个结点
cur.next = cur.next.next;
} else {
//否则cur向后移动
cur = cur.next;
}
}
return head;
}
leetcode203 移除链表元素
删除元素要从他的前一个元素操作
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while (cur.next != null) {
if (cur.next.val == val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
leetcode82 删除排序中的重复元素二
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while (cur.next != null && cur.next.next != null) {
int val = cur.next.val;
if (cur.next.next.val == val) {
while (cur.next != null && cur.next.val == val) {
cur.next = cur.next.next;
}
} else {
cur = cur.next;
}
}
return dummy.next;
}
leetcode19 删除倒数第n个结点
//方法一:双指针
public ListNode removeNthFromEnd(ListNode head, int n) {
//双指针,先初始化
ListNode dummy = new ListNode(0, head);
ListNode right = dummy;
while (n-- > 0) {
right = right.next;
}
ListNode left = dummy;
while (right.next != null) {
left = left.next;
right = right.next;
}
left.next = left.next.next;
return dummy.next;
}
//方法二:倒序遍历,正序删除
public ListNode removeNthFromEnd(ListNode head, int n) {
int len = 0;
ListNode dummy = new ListNode(0, head);
ListNode index = dummy;
while (index.next != null) {
len++;
index = index.next;
}
ListNode cur = dummy;
int t = len - n;
while (t-- > 0) {
cur = cur.next;
}
cur.next = cur.next.next;
return dummy.next;
}
leetcode160链表相交
给你两个单链表的头节点 headA 和 headB ,
请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
leetcode141 环形链表
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode index1 = head;
ListNode index2 = head;
while (index2 != null && index2.next != null) {
index1 = index1.next;
index2 = index2.next.next;
if (index1 == index2) {
return true;
}
}
return false;
}
leetcode142 环形链表二
思路分析:
x = 起点到入口
y = 入口到相遇点
z = 圈内剩余距离slow = x+y
fast = x+y+n(y+z)2(x+y) = x+y+n(y+z)
x = n(y+z)-y
x = (n-1)(y+z)+z
如果n=1则x=z
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode index1 = head, index2 = head;
while (index2 != null && index2.next != null) {
index1 = index1.next;
index2 = index2.next.next;
if (index2 == index1) {
//判断入口
ListNode slow = head;
ListNode fast = index1;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
return null;
}
}
leetcode234 回文链表
public boolean isPalindrome(ListNode head) {
//用栈来模拟翻转链表
Stack<Integer> stack = new Stack();
//记录长度
int len = 0;
ListNode cur = head;
while (cur != null) {
stack.push(cur.val);
cur = cur.next;
len++;
}
//进行出栈操作
len = len / 2;
while (len-- > 0) {
if (head.val != stack.pop()) {
return false;
}
head = head.next;
}
return true;
}
也可以通过双指针进行判断
通过快慢指针找到中间节点,翻转后半部分链表,再依次进行比较
leetcode328 奇偶链表
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode odd = head;
ListNode even = head.next;
//这个结点的作用是为了最后将偶数结点挂接到奇数结点之后
ListNode evenHead = even;
while (odd.next != null && even.next != null) {
//维护奇数结点串联
odd.next = even.next;
odd = odd.next;
//维护偶数结点串联
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
leetcode86 分隔链表
思路:重新定义两个链表进行拼接
public ListNode partition(ListNode head, int x) {
if (head == null || head.next == null) {
return head;
}
ListNode smallHead = new ListNode(0);
ListNode bigHead = new ListNode(0);
ListNode small = smallHead;
ListNode big = bigHead;
ListNode cur = head;
while (cur != null) {
if (cur.val < x) {
smallHead.next = cur;
smallHead = smallHead.next;
} else {
bigHead.next = cur;
bigHead = bigHead.next;
}
cur = cur.next;
}
bigHead.next = null;
smallHead.next = big.next;
return small.next;
}
leetcode148 排序链表
public ListNode sortList(ListNode head) {
if (head == null) {
return null;
}
ListNode res = head;
ArrayList<Integer> list = new ArrayList<>();
while (res != null) {
list.add(res.val);
res = res.next;
}
ListNode dummy = new ListNode(0, head);
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
head.val = list.get(i);
head = head.next;
}
return dummy.next;
}
leetcode146 LRU缓存
package com.sfy.LinkedListPractice;
import java.util.HashMap;
import java.util.Map;
public class LRUCache {
//函数 get 和 put 必须以 O(1) 的平均时间复杂度运行所以必须用双向链表
private class Node {
int key, val;
Node pre, next;
Node(int key, int val) {
this.key = key;
this.val = val;
}
}
//初始化容量
private final int capacity;
Node dummy = new Node(0, 0);
Map<Integer, Node> keyNode = new HashMap<>();
public LRUCache(int capacity) {
this.capacity = capacity;
dummy.pre = dummy;
dummy.next = dummy;
}
public int get(int key) {
Node node = getNode(key);
return node == null ? -1 : node.val;
}
public void put(int key, int value) {
//有就更新,没有就插入
Node node = getNode(key);
if (node != null) {
node.val = value;
return;
}
node = new Node(key, value);//新增
keyNode.put(key, node);
pushFront(node);//放在最前面
if (keyNode.size() > capacity) {
//容量过大就删除
Node pre = dummy.pre;
keyNode.remove(pre.key);
remove(pre);
}
}
private Node getNode(int key) {
if (!keyNode.containsKey(key)) {
return null;
}
Node node = keyNode.get(key);
remove(node);//先删除
pushFront(node);//再更新
return node;
}
private void pushFront(Node node) {
node.pre = dummy;
node.next = dummy.next;
node.pre.next = node;
node.next.pre = node;
}
private void remove(Node node) {
node.pre.next = node.next;
node.next.pre = node.pre;
}
}
leetcode61 旋转链表
思路一:先变成环再找新的头结点
思路二:1.全翻转 2.翻转前len-k 3.翻转后k个
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k == 0) {
return head;
}
int len = 1;
ListNode cur = head;
while (cur.next != null) {
len++;
cur = cur.next;
}
// k = 5 - 2 - 1 = 2
k = len - (k % len) - 1;
cur.next = head;//变成环形链表
for (int i = 0; i < k; i++) {
head = head.next;
}
//他的下一个位置就是新的头结点
ListNode newHead = head.next;
//将尾节点置空
head.next = null;
return newHead;
}
leetcode21 合并两个有序链表
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
ListNode newHead = new ListNode();
ListNode cur = newHead;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
if (list1 != null) {
cur.next = list1;
}
if (list2 != null) {
cur.next = list2;
}
return newHead.next;
}
leetcode25 k个一组翻转链表
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dummy = new ListNode(0,head);
ListNode pre = dummy;
ListNode end = dummy;
while(end.next!=null){
for(int i = 0;i<k&& end !=null;i++){//找到要反转的尾部
end = end.next;
}
if(end == null){
break;
}
ListNode start = pre.next;//找到要翻转的头部节点
ListNode nextStart = end.next;//找到下一个要反转的头结点,用于连接
end.next = null;
pre.next = reverse(start);
start.next=nextStart;
pre = start;
end = pre;
}
return dummy.next;
}
private ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}
}