/**
* // This is the ImmutableListNode's API interface.
* // You should not implement it, or speculate about its implementation.
* class ImmutableListNode {
* public:
* void printValue(); // print the value of the node.
* ImmutableListNode* getNext(); // return the next node.
* };
*/
class Solution {
public:
void printLinkedListInReverse(ImmutableListNode* head) {
}
};
/* Below is the interface for ImmutableListNode, which is already defined for you.
*
* type ImmutableListNode struct {
*
* }
*
* func (this *ImmutableListNode) getNext() ImmutableListNode {
* // return the next node.
* }
*
* func (this *ImmutableListNode) printValue() {
* // print the value of this node.
* }
*/
func printLinkedListInReverse(head ImmutableListNode) {
current := head
for current != nil {
defer current.printValue()
current = current.getNext()
}
}
/**
* // This is the ImmutableListNode's API interface.
* // You should not implement it, or speculate about its implementation.
* function ImmutableListNode() {
* @ return {void}
* this.printValue = function() { // print the value of this node.
* ...
* };
*
* @return {ImmutableListNode}
* this.getNext = function() { // return the next node.
* ...
* };
* };
*/
/**
* @param {ImmutableListNode} head
* @return {void}
*/
var printLinkedListInReverse = function(head) {
if (head === null)
return;
let fastBegin = head.getNext();
while (fastBegin !== null) {
let slow = head;
let fast = fastBegin;
while (fast !== null) {
slow = slow.getNext();
fast = fast.getNext();
}
slow.printValue();
fastBegin = fastBegin.getNext();
}
head.printValue();
};
# """
# This is the ImmutableListNode's API interface.
# You should not implement it, or speculate about its implementation.
# """
# class ImmutableListNode:
# def printValue(self) -> None: # print the value of this node.
# def getNext(self) -> 'ImmutableListNode': # return the next node.
class Solution:
def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None:
if head is None:
return
fast_begin = head.getNext();
while fast_begin is not None:
slow = head
fast = fast_begin
while fast is not None:
slow = slow.getNext()
fast = fast.getNext()
slow.printValue()
fast_begin = fast_begin.getNext()
head.printValue()
/**
* // This is the ImmutableListNode's API interface.
* // You should not implement it, or speculate about its implementation.
* interface ImmutableListNode {
* public void printValue(); // print the value of this node.
* public ImmutableListNode getNext(); // return the next node.
* };
*/
class Solution {
public void printLinkedListInReverse(ImmutableListNode head) {
if (head == null)
return;
ImmutableListNode fastBegin = head.getNext();
while (fastBegin != null) {
ImmutableListNode slow = head;
ImmutableListNode fast = fastBegin;
while (fast != null) {
slow = slow.getNext();
fast = fast.getNext();
}
slow.printValue();
fastBegin = fastBegin.getNext();
}
head.printValue();
}
}
/**
* // This is the ImmutableListNode's API interface.
* // You should not implement it, or speculate about its implementation.
* class ImmutableListNode {
* public:
* void printValue(); // print the value of the node.
* ImmutableListNode* getNext(); // return the next node.
* };
*/
class Solution {
public:
void printLinkedListInReverse(ImmutableListNode* head) {
if (head == nullptr)
return;
ImmutableListNode* fast_begin = head->getNext();
while (fast_begin != nullptr) {
ImmutableListNode* slow = head;
ImmutableListNode* fast = fast_begin;
while (fast != nullptr) {
slow = slow->getNext();
fast = fast->getNext();
}
slow->printValue();
fast_begin = fast_begin->getNext();
}
head->printValue();
}
};
/**
* // This is the ImmutableListNode's API interface.
* // You should not implement it, or speculate about its implementation.
* interface ImmutableListNode {
* public void printValue(); // print the value of this node.
* public ImmutableListNode getNext(); // return the next node.
* };
*/
class Solution {
public void printLinkedListInReverse(ImmutableListNode head) {
part(head, null);
}
// 这里区间是左闭右开
private void part(ImmutableListNode start, ImmutableListNode end) {
if (start.getNext() == end) {
// 由于左闭右开,此时区间中只剩一个节点了,打印并结束递归
start.printValue();
return;
}
// 使用快慢指针找到中间节点
ImmutableListNode mid;
for (ImmutableListNode p = start, q = start; ; p = p.getNext(), q = q.getNext().getNext()) {
if (q == end || q.getNext() == end) {
mid = p;
break;
}
}
// 因为要逆序,所以先递归后半部分
part(mid, end);
part(start, mid);
}
}