使用 虚拟头节点 可以有效解决该问题,剩下的只是模拟就好
/**
* 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 swapPairs(ListNode head) {
if(head == null || head.next == null) return head;
ListNode dummy = new ListNode(-1,head);
ListNode p = dummy;
ListNode q = head;
//画图模拟,轻松写出
while(p.next != null && q.next != null){
p.next = q.next;
p = p.next;
q.next = p.next;
p.next = q;
p = p.next;
q = q.next;
}
return dummy.next;
}
}
- 利用双指针,让快指针先走
n + 1
步
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//新建一个虚拟头节点指向head
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
//快慢指针指向虚拟头节点
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
// 只要快慢指针相差 n 个结点即可
for (int i = 0; i <= n; i++) {
fastIndex = fastIndex.next;
}
while (fastIndex != null) {
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
// 此时 slowIndex 的位置就是待删除元素的前一个位置。
// 具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
// 检查 slowIndex.next 是否为 null,以避免空指针异常
if (slowIndex.next != null) {
slowIndex.next = slowIndex.next.next;
}
return dummyNode.next;
}
}
- 或者笨方法(我自己写的)先遍历一边,然后大体思路和快指针先走
n+1
步一致
/**
* 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 removeNthFromEnd(ListNode head, int n) {
if(head == null) return head;
int size = 0;
ListNode dummy = new ListNode(0,head);
ListNode cur = dummy;
while(cur!= null){
size ++;
cur = cur.next;
}
int count = size - n;
ListNode pos = dummy;
ListNode prev = dummy;
while(count > 0){
prev = pos;
pos = pos.next;
count --;
}
prev.next = pos.next;
return pos == head? head.next : head;
}
}
面试题 02.07. 链表相交
- 让两个链表的尾部对齐,也就是先计算长度差值
n
,长的先向前走 n 个节点
/**
* 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) {
int sizeA = 0;
int sizeB = 0;
ListNode p,q;
while(p!=null){
sizeA++;
p = p.next;
}
p = headB;
while(p!=null){
sizeB++;
p = p.next;
}
int gap = sizeA - sizeB > 0 ? sizeA - sizeB : sizeB - sizeA;
int count = gap;
if(sizeA > sizeB){
p = headA;
while(count > 0){
p = p.next;
count --;
}
q = headB;
while(p != null && q != null){
if(p == q)return q;
p = p.next;
q = q.next;
}
}else{
p = headB;
while(count > 0){
p = p.next;
count --;
}
q = headA;
while(p != null && q != null){
if(p == q)return q;
p = p.next;
q = q.next;
}
}
return null;
}
}
142. 环形链表 II
- 可以直接用HashSet做,非常简单
- 用指针做
我的思路,超限了
/**
* 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) {
if(head == null || head.next == null) return null;
ListNode fast = head;
ListNode slow = head;
ListNode pos = head;
ListNode cur = null;
//判断是否有环
while(fast != null){
fast = fast.next.next;
if(fast == slow){
cur = slow;
}
slow = slow.next;
}
//没有环 返回null
if(cur == null) return null;
//设置指针q 为 环中一个节点
ListNode q = cur;
//pos此时指向头节点,如果环中的一点是头节点,则直接返回
if(pos == cur) return pos;
//pos在每次cur循环一圈时 前进到下一个节点
while( q != null){
//此时 q 在环中运动,pos在q每次绕环一圈 运动一次
q = q.next;
//查看q和pos是否相交
if(q == pos) return q;
if(q == cur) pos = pos.next;
}
return q;
}
}
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) {// 有环
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}