**input: **a single linked list output: a list node head of the reverse list of given input **corner: **when the list is null, or only contains one head

What we want to do is to reverse the list. So we scan from head to tail. For every two nodes `cur` and `cur.next`, we will make cur point to its forward node.

So at each step, we need a node `pre` to keep connection with current scanner, so that `cur.next = pre`. And we also need a node `nextOne` to memorize cur.next, since it will be changed during the scanner, if without track, `cur` will lose its way to next scanner.

In order to move to next scanner, `pre` will move to `cur`, and `cur` will move to `nextOne`.

Until `cur` moves to tail null, `pre` is currently the new head of the reversed list, so just return it.

The idea of `Recursion` is similar with `Iteration`, what to do in current scanner is to keep track of `nextOne` and point to `pre`, and what to prepare for next step is to pass `nextOne` and `cur` as the new 'cur' and 'pre'.

```//iteration
public class Solution{
//corner
}

ListNode pre = null;

while ( cur != null ){
//reverse
ListNode nextOne = cur.next;
cur.next = pre;
//prepare
pre = cur;
cur = nextOne;
}

return pre;
}
}

//recursion
public class Solution{
//corner
}

ListNode pre = null;

}

public ListNode helper(ListNode cur, ListNode pre){
//base
if ( cur == null ){
return pre;
}

//current
ListNode nextOne = cur.next;
cur.next = pre;

//next
return helper(nextOne, cur);
}
}```

0 条评论

## 相关文章

76850

12650

21630

7810

289100

### 在Go中使用服务对象模式

NOTE: Most of the code and ideas in this post are things I have been experimenti...

11320

### Jar Mismatch! Fix Your Dependencies

There was a requirement of my work. It requires me to integrated my current proj...

10120

58580

### SharpForge - Open source SourceForge / CodePlex implementation

SharpForge - Open source SourceForge / CodePlex implementation SharpForge suppo...

217100

72650