commit e95e2099b07af4dd4d647795e3585fc9ca7636f5 parent 0a3c7b15a6db17c131535771145aa39ffe032294 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sun, 14 May 2023 20:36:13 -0500 Completed swapping nodes in a linked list using dart Diffstat:
| A | swapping-nodes-in-a-linked-list/swapping-nodes-in-a-linked-list.dart | | | 29 | +++++++++++++++++++++++++++++ |
1 file changed, 29 insertions(+), 0 deletions(-)
diff --git a/swapping-nodes-in-a-linked-list/swapping-nodes-in-a-linked-list.dart b/swapping-nodes-in-a-linked-list/swapping-nodes-in-a-linked-list.dart @@ -0,0 +1,29 @@ +//Swap two nodes in a linked list without swapping the values. +//Basically place the kth element in the length - kth spot +//and vice versa not just swap the stored values. +//I did this using an array list where I swapped the nodes +//and then went through the list setting the next node to be +//n + 1. This was the easiest solution I could think of. +//The time complexity of this is O(n) where n is the lenght of the +//list. +//Time: 465ms Beats: 50% +//Memory: 212.2MB Beats: 25% +class Solution { + ListNode? swapNodes(ListNode? head, int k) { + ListNode? itr = head; + List<ListNode?> vals = []; + while(itr != null){ + vals.add(itr); + itr = itr.next; + } + + ListNode? temp = vals[k - 1]; + vals[k - 1] = vals[vals.length - k]; + vals[vals.length - k] = temp; + for(int i = 0 ; i < vals.length - 1 ; ++i){ + vals[i]?.next = vals[i + 1]; + } + vals[vals.length - 1]?.next = null; + return vals[0]; + } +}