Reverse a singly linked list
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public class reverseList {
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while(head!=null){
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
}
本文介绍了一种简单有效的方法来反转单向链表。通过迭代方式重新连接链表节点,最终实现链表的反转。该算法易于理解和实现。
1254

被折叠的 条评论
为什么被折叠?



