Home [Medium] Swapping Nodes in a Linked List
Post
Cancel

[Medium] Swapping Nodes in a Linked List

‘Swapping Nodes in a Linked List (Medium)`


📌 Problem

https://leetcode.com/problems/swapping-nodes-in-a-linked-list/

📌 Answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
 * 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 swapNodes(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;
        ListNode first = head, second = head;
        
		// Put fast (k-1) nodes after slow
        for (int i = 1; i < k; i++)
            fast = fast.next;
            
		// Save the node for swapping
        first = fast;

		// Move until the end of the list
        while (fast.next != null) {
			slow = slow.next;
            fast = fast.next;
        }
        
        // Save the second node for swapping
		// Note that the pointer second isn't necessary: we could use slow for swapping as well
		// However, having second improves readability
        second = slow;
		
		// Swap values
        int temp = first.val;
        first.val = second.val;
        second.val = temp;
        
        return head;
    }
}
Reference
https://leetcode.com/problems/swapping-nodes-in-a-linked-list/solutions/1009918/java-two-pointers-detailed-explanation-o-n-time-o-1-space/?orderBy=most_votes&languageTags=java
This post is licensed under CC BY 4.0 by the author.

[Development] MSA

[Development] Apache / Spring Utils

Comments powered by Disqus.