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.)
[0, 100].0 <= Node.val <= 100No developer has claimed the Java throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next./**
* 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) {
var dummyHead = new ListNode(0, head);
var previous = dummyHead;
var current = head;
while (current != null && current.next != null) {
var nextNext = current.next.next;
previous.next = current.next;
current.next.next = current;
current.next = nextNext;
previous = current;
current = nextNext;
}
return dummyHead.next;
}
}
No developer has claimed the Python throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next.# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy_head = ListNode(next=head)
previous = dummy_head
current = head
while current and current.next:
next_next = current.next.next
previous.next = current.next
current.next.next = current
current.next = next_next
previous = current
current = next_next
return dummy_head.next
No developer has claimed the C++ throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next./**
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
auto dummy_head = new ListNode(0, head);
auto previous = dummy_head;
auto current = head;
while (current != nullptr && current->next != nullptr) {
auto next_next = current->next->next;
previous->next = current->next;
current->next->next = current;
current->next = next_next;
previous = current;
current = next_next;
}
auto result = dummy_head->next;
delete dummy_head;
return result;
}
};
No developer has claimed the Javascript throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next./**
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
var swapPairs = function (head) {
const dummyHead = new ListNode(0, head)
let previous = dummyHead
let current = head
while (current != null && current.next != null) {
const nextNext = current.next.next
previous.next = current.next
current.next.next = current
current.next = nextNext
previous = current
current = nextNext
}
return dummyHead.next
};
No developer has claimed the C# throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next./**
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution
{
public ListNode SwapPairs(ListNode head)
{
var dummyHead = new ListNode(0, head);
var previous = dummyHead;
var current = head;
while (current != null && current.next != null)
{
var nextNext = current.next.next;
previous.next = current.next;
current.next.next = current;
current.next = nextNext;
previous = current;
current = nextNext;
}
return dummyHead.next;
}
}
No developer has claimed the Go throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next./**
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func swapPairs(head *ListNode) *ListNode {
dummyHead := &ListNode{0, head}
previous := dummyHead
current := head
for current != nil && current.Next != nil {
nextNext := current.Next.Next
previous.Next = current.Next
current.Next.Next = current
current.Next = nextNext
previous = current
current = nextNext
}
return dummyHead.Next
}
No developer has claimed the Ruby throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Before solving this problem, it is recommended to solve the simple problem 206. Reverse Linked List first.
current and previous.while (current.next != null) instead of while (current != null), because the operations that need to be performed include current.next.next.# class ListNode
# attr_accessor :val, :next
#
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
def swap_pairs(head)
dummy_head = ListNode.new
dummy_head.next = head
previous = dummy_head
current = head
while current && current.next
next_next = current.next.next
previous.next = current.next
current.next.next = current
current.next = next_next
previous = current
current = next_next
end
dummy_head.next
end
No developer has claimed the Rust throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #24 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.