-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse Linked List.swift
52 lines (43 loc) · 1.45 KB
/
Reverse Linked List.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// https://leetcode.com/problems/reverse-linked-list/?envType=study-plan-v2&envId=leetcode-75
// Given the head of a singly linked list, reverse the list, and return the reversed list
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
public class ListNode {
public var val: Int
public var next: ListNode?
public init() { self.val = 0; self.next = nil; }
public init(_ val: Int) { self.val = val; self.next = nil; }
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
}
class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
var resultNode: ListNode?
var current: ListNode? = head
while current != nil {
let temp = current?.next
current?.next = resultNode
resultNode = current
current = temp
}
return resultNode
}
}
let node3 = ListNode(3)
let node2 = ListNode(2, node3)
let head = ListNode(1, node2)
let solution = Solution()
let reversedHead = solution.reverseList(head)
// Printing the reversed linked list
var current = reversedHead
while current != nil {
print(current!.val)
current = current?.next
}