24. Swap Nodes in Pairs
Given the head of a singly linked list, swap every two adjacent nodes and return its head.
You cannot modify the values in the nodes — only rearrange the nodes themselves
How the Code Works (Key Technique)
- Dummy Node: A dummy node is placed before the actual head to handle swaps at the beginning easily.
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){return head;}
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode curr = dummyHead;
while(curr.next != null && curr.next.next != null){
ListNode temp = curr.next;
ListNode temp1 = curr.next.next.next;
curr.next = curr.next.next;
curr.next.next = temp;
curr.next.next.next = temp1;
curr = curr.next.next;
}
return dummyHead.next;
}
}
19. Remove Nth Node From End of List
Given the head of a singly linked list, remove the nth node from the end of the list and return the updated head of the list.
How the Code Works (Key Technique)
- Dummy Node: A dummy node is placed before the actual head to handle swaps at the beginning easily.
- Two-Pointer: Use two pointers (
fasterNodeandslowerNode). MovefasterNodeforward byn+1steps to maintain a gap ofnnodes between the two pointers.
Complexity
- Time Complexity:
O(n)— where n is the length of the linked list. The list is traversed once. (Brute force will need 2n) - Space Complexity:
O(1)— only a constant amount of extra space is used (for pointers and the dummy node).
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode fasterNode = dummyHead, slowerNode = dummyHead;
for(int i = 0; i < n+1; i++){
fasterNode = fasterNode.next;
}
while(fasterNode != null){
fasterNode = fasterNode.next;
slowerNode = slowerNode.next;
}
slowerNode.next = slowerNode.next.next;
return dummyHead.next;
}
}
160. Intersection of Two Linked Lists
Given the heads of two singly-linked lists, determine whether they intersect (i.e. share a common tail node). If they do, return the first shared node; otherwise return null.
How the Code Works (Key Technique)
- Measure Lengths
- Align the Lists
- Traverse in Tandem: Move both pointers forward one node at a time. At each step, check if
currA == currB. If so, you’ve found the intersection and return it.
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode currA = headA;
ListNode currB = headB;
int lengthA = 0;
int lengthB = 0;
while(currA !=null){
currA = currA.next;
lengthA++;
}
while(currB !=null){
currB = currB.next;
lengthB++;
}
int diff = 0;
int restN = 0;
currA = headA;
currB = headB;
if(lengthA > lengthB){
diff = lengthA - lengthB;
for(int i=0; i<diff; i++){
currA = currA.next;
}
restN = lengthB;
}else{
diff = lengthB - lengthA;
for(int i=0; i<diff; i++){
currB = currB.next;
}
restN = lengthA;
}
for(int i=0; i<restN; i++){
if(currA == currB){
return currA;
}else{
currA = currA.next;
currB = currB.next;
}
}
return null;
}
}
142. Linked List Cycle II
Given the head of a singly linked list, determine whether it contains a cycle—and if so, return the node where the cycle begins; otherwise, return null.
How the Code Works (Key Technique)
- Cycle Detection: Two pointers—
currSlow(moves one step at a time) andcurrFast(moves two steps at a time)—traverse the list. If there is no cycle,currFastorcurrFast.nextwill hitnulland we returnnull. If they ever meet, a cycle exists. - Finding the Cycle’s Entry Point:
Once
currSlowandcurrFastmeet inside the cycle, initialize two pointers. index1 at the head of the list, index2 at the meeting point(currFast). Add both one step at a time. They will first coincide exactly at the start of the cycle. That meeting node is returned as the cycle’s entry.(this concept can be prove by math)
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode currFast = head;
ListNode currSlow = head;
while(currFast != null && currFast.next != null){
currFast = currFast.next.next;
currSlow = currSlow.next;
if(currFast == currSlow){
ListNode index1 = head;
ListNode index2 = currFast;
while(index1 != index2){
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}
851

被折叠的 条评论
为什么被折叠?



