Reserve List
Reverse Linked List
๐ขlc206
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
var pre *ListNode
cur := head
for cur != nil {
pre, cur, cur.Next = cur, cur.Next, pre
}
return pre
}
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let (mut pre, mut cur) = (None, head);
while let Some(mut node) = cur {
cur = node.next;
node.next = pre;
pre = Some(node);
}
pre
}
}
Reverse Linked List II
๐กlc92
Reverse Nodes in k-Group
๐ดlc25
Reverse Nodes in Even Length Groups
๐กlc2074