swift - Extracting values from a reversed linked list -
question:
you have 2 numbers represented linked list, each node contains single digit. digits stored in reverse order, such 1 's digit @ head of list. write function adds 2 numbers , returns sum linked list.
an example:
input: (7-> 1 -> 6) + (5 -> 9 -> 2).
that is: 617 + 295.
output: 2 -> 1 -> 9.
that is: 912.
in order begin question, first created class define linked list:
step 1: defining linked list
class node: customstringconvertible{ var value: int var next: node? var description: string{ if next != nil { return "\(value) -> \(next!)" } else{ return "\(value) -> \(next)" } } init(value: int) { self.value = value } }
step: 2 - generated linked list, user input of integer values
func generatelist (num: int) -> node { var stringnum = array(string(num).characters) let head = node.init(value:int(string(stringnum.first!))!) var current = head in 1..<stringnum.count{ let num = int(string(stringnum[i])) current.next = node.init(value: num!) current = current.next! } return head } let list = generatelist(num: 716) // following prints out: 7 -> 1 -> 6 -> nil
then proceeded on reverse linked list using following function.
step 3: reverse linked list
func reverselinkedlist (head: node?) -> node?{ var current = head var prev: node? var next: node? while current != nil { next = current?.next current?.next = prev prev = current current = next } return prev } let reversedlist = reverselinkedlist(head: list) // following prints out is: 6 -> 1 -> 7 -> nil
step 4: idea behind step extract values on each of nodes, cast them string , concatenate them string variable , lastly cast string value int , use int value , add them.
func getvaluesfrom (head: node?) -> int { var string = "" var current = head while current != nil { var stringval = string(describing: current?.value) string += stringval current = current?.next } return int(string)! }
here having problem:
when plug in following function so:
getvaluesfrom(head: reversedlist)
i following error:
fatal error: unexpectedly found nil while unwrapping optional value
and can't seem figure out why having problem , appreciate sort of insight.
there no need convert , forth between string , linked list, except print results. done this:
class node { var value: int var next: node? // init , generator can same method init(value: int) { // store ones place , divide 10 self.value = value % 10 var nextvalue = value / 10 // set loop var currentnode = self while nextvalue > 0 { // create new node // store ones place , divide 10 let next = node(value: nextvalue % 10) nextvalue /= 10 // make new node next node currentnode.next = next // set next iteration currentnode = next } } } // make list printable extension node: customstringconvertible { var description: string{ if let next = next { return "\(value) -> \(next)" } else { return "\(value) -> nil" } } }
now can do:
print(node(value: 671)) // prints "7 -> 1 -> 6 -> nil"
there no need reverse lists, given question.
to sum lists can you've said, convert int
, add them, generate new list:
extension node { func tovalue() -> int { var place = 10 var current = self // add each value , multiply place value // first 1, second 10, third 100, etc. var result = current.value while let next = current.next { result += next.value * place place *= 10 current = next } return result } }
then need overload addition operator...
func +(lhs: node, rhs: node) -> node { return node(value: lhs.tovalue() + rhs.tovalue()) }
and test...
let first = node(value: 617) let second = node(value: 295) print(first) print(second) print(first + second)
result:
7 -> 1 -> 6 -> nil
5 -> 9 -> 2 -> nil
2 -> 1 -> 9 -> nil
Comments
Post a Comment