力扣:206. 反向列表
This commit is contained in:
parent
bf339bbbea
commit
339aafab1b
@ -0,0 +1,27 @@
|
||||
package com.code.leet.study.t20210207;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
/**
|
||||
* 反转一个单链表。
|
||||
* <p>
|
||||
* 示例:
|
||||
* <p>
|
||||
* 输入: 1->2->3->4->5->NULL
|
||||
* 输出: 5->4->3->2->1->NULL
|
||||
* 进阶:
|
||||
* 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
|
||||
*/
|
||||
public class ReverseList {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode prev = null;
|
||||
ListNode curr = head;
|
||||
while (curr != null) {
|
||||
ListNode next = curr.next;
|
||||
curr.next = prev;
|
||||
prev = curr;
|
||||
curr = next;
|
||||
}
|
||||
return prev;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user