leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit e751cd71adc7878b889d9f17dd6bb2e47228c9f8
parent e8d7905feffd7e8c18e490bc02fb20b3545b2d28
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Mon, 15 May 2023 20:57:57 -0500

Completed swap nodes in pais with dart

Diffstat:
Aswap-nodes/swap-nodes.dart | 30++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+), 0 deletions(-)

diff --git a/swap-nodes/swap-nodes.dart b/swap-nodes/swap-nodes.dart @@ -0,0 +1,30 @@ +//Swap every other node in a list then return the head. +//I did this the easy way by putting them into an array swapping +//them and setting the next val to the next. +//The time complexity if O(n) where n is the lenght of the list. +//Time: 258ms Beats: 77.78% +//Memory: 144.5MB Beats: 22.22% +class Solution { + ListNode? swapPairs(ListNode? head) { + if(head == null || head?.next == null){ + return head; + } + List<ListNode?> nodes = []; + while(head != null){ + nodes.add(head); + head = head.next; + } + int itr = 0; + while(itr < nodes.length - 1){ + ListNode? temp = nodes[itr]; + nodes[itr] = nodes[itr + 1]; + nodes[itr + 1] = temp; + itr += 2; + } + for(int i = 0 ; i < nodes.length - 1; ++i){ + nodes[i]?.next = nodes[i + 1]; + } + nodes[nodes.length - 1]?.next = null; + return nodes[0]; + } +}