Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Farouk Elabady
Swap the values instead of references

Farouk Elabady

Jul 27, 2024

In this solution I swapped the values instead of the references which is simpler , It may not be the correct approach to get familiar with LinkedList

Solution { public ListNode swapPairs(ListNode head) { // TODO: Write your code hereu ListNode current = head; while(current != null && current.next != null) { int t = current.val; current.val = current.next.val; current.next.val = t; current = current.next.next; } return head; } }

0

0

Comments
Comments

On this page