leetcode

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

commit 86cb2f9baa158a1f511a2697e38ef27afe49981f
parent 92277f1ab4946d5f977afe2d85fa0197eab06a3a
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Fri, 21 Apr 2023 15:25:37 -0500

Reverse linked list in dart

Diffstat:
Areverse-linked-list/reverse-list.dart | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/reverse-linked-list/reverse-list.dart b/reverse-linked-list/reverse-list.dart @@ -0,0 +1,40 @@ +//This algorithm reverses the order of a linked list. +//To do this it puts all of the values into a list and then +//iterates through the list in reverse order placing each iterated value +//into a new linked list. From there it returns that head of the new list. +//The time complexity of this is O(n) where n is the length of the list. +//Runtime: 239ms Beats: 93.94% +//Memory: 142.5MB Beats: 77.78% + +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode? next; + * ListNode([this.val = 0, this.next]); + * } + */ +class Solution { + ListNode? reverseList(ListNode? head) { + if(head == null){ + return null; + } + + List<ListNode?> nodes = []; + while(head != null){ + nodes.add(head); + head = head.next; + } + ListNode? ln1 = new ListNode(); + ln1.val = nodes[nodes.length - 1]?.val ?? 0; + head = ln1; + ListNode? ln2; + for(int i = nodes.length - 2 ; i >= 0 ; --i){ + ln2 = new ListNode(); + ln2.val = nodes[i]?.val ?? 0; + ln1?.next = ln2; + ln1 = ln2; + } + return head; + } +}