#import <Foundation/Foundation.h>
@interface ListNode : NSObject {
NSInteger _value;
ListNode *_nextNode;
}
@end
@implementation ListNode
- (void)printfValue {
printf("%d ",(int)_value);
}
- (void)setValue:(NSInteger)value {
_value = value;
}
- (void)setNextNode:(ListNode *)node {
_nextNode = node;
}
- (ListNode *)getNextNode {
return _nextNode;
}
@end
@interface Solution : NSObject
@end
@implementation Solution
+ (ListNode *)reverseList:(ListNode *)head {
ListNode *preNode = head;
ListNode *currentNode = [head nextNode];
while ([currentNode nextNode] != nil) {
currentNode.nextNode = preNode;
preNode = currentNode;
currentNode = [currentNode nextNode];
}
return currentNode;
}
@end
int main(int argc, char* argv[]) {
NSInteger i = 0;
ListNode *preNode = nil;
ListNode *headNode = nil;
while (i < 5) {
ListNode *node = [[ListNode alloc] init];
node.value = i;
preNode.nextNode = node;
preNode = node;
[node printfValue];
if (headNode == nil) {
headNode = node;
}
i++;
}
headNode = [Solution reverseList:headNode];
return 0;
}