24. Swap Nodes in Pairs
Link: https://leetcode.com/problems/swap-nodes-in-pairs/
Description
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.
Solution
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1, head);
ListNode cur = dummyHead;
ListNode temp1 = null;
ListNode temp2 = null;
ListNode temp3 = null;
while (cur.next != null && cur.next.next != null) {
temp1 = cur.next;
temp2 = cur.next.next;
temp3 = cur.next.next.next;
cur.next = temp2;
temp2.next = temp1;
temp1.next = temp3;
cur = temp1;
}
return dummyHead.next;
}
}
//Recursion
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode next = head.next;
ListNode temp = swapPairs(next.next);
next.next = head;
head.next = temp;
return next;
}
}
Remark
Use dummy head.
19. Remove Nth Node From End of List
Link: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
Description
Given the head
of a linked list, remove the nth node from the end of the list and return its head.
Approach
Two pointers
- Initialize
fast
pointer andslow
pointer, both points to thehead
. - Let
fast
points to the nth node. - Traverse both
fast
andslow
pointers, untilfast
pointer reaches the end of the list. - Let the
slow
pointer points to the next of next pointer, delete the next pointer.
Solution
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(-1, head);
ListNode fast = dummyHead;
ListNode slow = dummyHead;
for (int i = 0; i < n; i++)
fast = fast.next;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummyHead.next;
}
}
160. Intersection of Two Linked Lists
Link: https://leetcode.com/problems/intersection-of-two-linked-lists/
Description
Given the heads of two singly linked-lists headA
and headB
, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null
.
For example, the following two linked lists begin to intersect at node c1
:
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Approach
- Compute the length of A and B.
- Let longer length points to the place where the reamin length equals to the shorter length.
- Iterate the two lists until the two lists findsame node.
Code
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA = 0;
int lenB = 0;
ListNode curA = headA;
ListNode curB = headB;
while (curA != null) {
lenA++;
curA = curA.next;
}
while (curB != null) {
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
if (lenA < lenB) {
ListNode temp = curB;
curB = curA;
curA = temp;
int tempL = lenB;
lenB = lenA;
lenA = tempL;
}
int len = lenA - lenB;
while (len > 0) {
curA = curA.next;
len--;
}
while (curA != null) {
if (curA == curB)
return curA;
curA = curA.next;
curB = curB.next;
}
return null;
}
}
142. Linked List Cycle II
Link: https://leetcode.com/problems/linked-list-cycle-ii/
Description
Given the head
of a linked list, return the node where the cycle begins. If there is no cycle, return null
.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next
pointer. Internally, pos
is used to denote the index of the node that tail’s next
pointer is connected to (0-indexed). It is -1
if there is no cycle. Note that pos
is not passed as a parameter.
Do not modify the linked list.
Approach
Floyd’s Cycle Detection (Two pointers)
Detect Cycle
- Initialize a
fast
pointer and aslow
pointer, both points to the head. - Let the
fast
pointer moves 2 steps at a time,slow
pointer moves 1 step at a time. - If the
fast
pointer andslow
pointer meets, then there exists a cycle.
Compute the start of the cycle
- Denote
x
as the length from head to the start of the cycle,y
as the length from the start of they cycle to the meet point,z
as the length from the meet point to the start of the cycle. - When they meet, slow pointer travels
x + y
nodes, fast pointer travelsx + y + n(y + z)
. Thus,(x + y) * 2 = x + y + n(y + z)
. - After simplification, the formula truns to
x = (n - 1) (y + z) + z
. - So, if a pointer at
head
and a pointer at the meet node start to move at the same time, they will meet at the start of the cycle.
Solution
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
ListNode n1 = head;
ListNode n2 = fast;
while (n1 != n2) {
n1 = n1.next;
n2 = n2.next;
}
return n1;
}
}
return null;
}
}
Reference: https://programmercarl.com/