Flashcards
Reverse Singly linked list
Flashcards do conjunto (2)
- 1Reverse a singly linked list iterative solution/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return head;
ListNode cur = head;
ListNode prev = null;
ListNode next;
while(cur != null){
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
} - 2Reverse singly linked list using recursionpuclic void ReverseList(ListNode p){
if(p== null)
return p;
ReverseList(p.next);
ListNode q = p.next;
q.next = p;
p.next = null;
}
Informações
- Descrição
- FlashCards sobre Reverse Singly linked list, criado por Suhas S em 12-02-2018.
- Criado por
- Suhas S
- Visualizações
- 10
- Tags
- Avaliação