| Question | Answer |
| Reverse 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; } } |
| Reverse singly linked list using recursion | puclic void ReverseList(ListNode p){ if(p== null) return p; ReverseList(p.next); ListNode q = p.next; q.next = p; p.next = null; } |
Want to create your own Flashcards for free with GoConqr? Learn more.