24. 两两交换链表中的节点 - 力扣(LeetCode)
迭代:使用虚拟头结点,然后明确迭代退出条件,当奇数节点时,curr.next==null,当偶数节点时,curr.next.next ==null。因为循环条件为curr.next !=null && curr.next.ext != null。使用三个指针pre、tail、temp分别保存curr.next、curr.next.next、curr.next.next.next。然后把pre指向下次需要交换的节点,tail.next指向pre。curr.nex指向tail。然后移动curr到下个位置(即curr = pre)。遍历完成后返回虚拟头结点的下个节点即可
public ListNode swapPairs(ListNode head) {
if (head == null) {
return head;
}
ListNode dummy = new ListNode();
dummy.next = head;
ListNode curr = dummy;
while (curr.next != null && curr.next.next != null) {
ListNode pre = curr.next;
ListNode tail = curr.next.next;
ListNode temp = tail.next;
pre.next = temp;
tail.next = pre;
curr.next = tail;
curr = pre;
}
return dummy.next;
}
时间复杂度:O(n),空间复杂度O(1)
递归:按照上面的思路,返回的应该是head.next ,head指向的下个要交换的节点(即newHead.next)。然后newHead下个节点指向head。
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null){
return head;
}
ListNode newHead = head.next;
head.next = swapPairs(newHead.next);
newHead.next = head;
return newHead;
}
时间复杂度:O(n),空间复杂度O(n)
19. 删除链表的倒数第 N 个结点 题解 - 力扣(LeetCode)
计算链表长度删除:
1、使用虚拟头节点,指向head,且使用curr指向虚拟头节点。
2、遍历链表获取长度size,size-n = 循环次数
3、使用新的curr1指向虚拟头节点,然后循环上面次数,curr1.next就是要删除的节点,删除元素后返回虚拟头节点.next.
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode curr = dummy;
int size = 0;
while (curr.next !=null){
size++;
curr = curr.next;
}
int count = size - n;
ListNode curr1 = dummy;
while (curr1.next !=null){
if (count == 0){
curr1.next = curr1.next.next;
break;
}
count--;
curr1 = curr1.next;
}
return dummy.next;
}
时间复杂度:O(2n),空间复杂度O(1)
双指针:
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
n++;
while (fast != null && n > 0) {
fast = fast.next;
n--;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
时间复杂度:O(n),空间复杂度O(1)
160. 相交链表 - 力扣(LeetCode)
长度差求相交:
1、遍历链表两个链表,获取他们长度。
2、求得长度差,如果是A长则让A先走长度差,否则B走
3、走完长度差后,那么A与B当前长度是一致的,遍历A或B,同时移动A、B当前指针,判定是否有相同节点即可
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int sizeA = 0, sizeB = 0;
ListNode currA = headA;
ListNode currB = headB;
while (currA != null) {
sizeA++;
currA = currA.next;
}
while (currB != null) {
sizeB++;
currB = currB.next;
}
ListNode result = null;
currA = headA;
currB = headB;
int n = sizeA - sizeB;
if(n > 0) {
for (int i = 0; i < n; i++) {
currA = currA.next;
}
} else {
n = -n;
for (int i = 0; i < n; i++) {
currB = currB.next;
}
}
while (currA != null) {
if (currA == currB) {
result = currA;
break;
}
currA = currA.next;
currB = currB.next;
}
return result;
}
时间复杂度:O(m+n),其中m,n分别为链表headA,headB的长度。
空间复杂度:O(1)
双指针:
1、定义currA指针,走完headA的节点,再走完headB的节点
2、定义currB指针,走完headB的节点,再走完headA的节点
3、过程中有相遇,则说明两个链表在此节点处相交;否则两个节点都为null,则说明两个链表不相交。(currA与currB都是A、B链表长度)
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode currA = headA;
ListNode currB = headB;
while (currA != currB) {
currA = currA == null ? headB : currA.next;
currB = currB == null ? headA : currB.next;
}
return currA;
}
时间复杂度:O(m+n),其中m,n分别为链表headA,headB的长度。
空间复杂度:O(1)
142. 环形链表 II - 力扣(LeetCode)
双指针:
1、使用快慢指针指向head,快指针走两步,慢指针走一步
2、循环快慢指针,如果链表有环,麻烦快慢指针一定会相遇,并且慢指针在第一圈环内就相遇
3、当快慢指针相遇时,相遇点到环入库与头节点到环入口相等,再求出入口即可
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
ListNode index1 = fast;
ListNode index2 = head;
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
时间复杂度:O(N),其中 N 为链表中节点的数目。在最初判断快慢指针是否相遇时, slow 指针走过的距离不会超过链表的总长度;随后寻找入环点时,走过的距离也不会超过链表的总长度。因此,总的执行时间为 O(N)+O(N)=O(N)。
空间复杂度:O(1)。我们只使用了 slow,fast,ptr 三个指针。
哈希表:
遍历链表时,同时使用哈希表存入节点,如果哈希表有之前存入的节点,那么该节点就是入口
public ListNode detectCycle(ListNode head) {
HashSet<ListNode> set = new HashSet<>();
while (head != null) {
if (set.contains(head)){
return head;
}else {
set.add(head);
head = head.next;
}
}
return null;
}
时间复杂度:O(N),其中 N 为链表中节点的数目。我们恰好需要访问链表中的每一个节点。
空间复杂度:O(N),其中 N 为链表中节点的数目。我们需要将链表中的每个节点都保存在哈希表当中。