DAY-3 Linked List
DAY-3 Linked List
Subject: IT Skills
Day 3 & 4:
Topics Covered: LINKED LIST
2.
public class Solution {
public ListNode insertionSortList(ListNode head) {
ListNode fakeHead = new ListNode(Integer.MIN_VALUE);
while (head != null) {
ListNode p = fakeHead;
while (p != null) {
Department of Computer Science and EngineeringPage 2
if (head.val >= p.val && (p.next == null || head.val <= p.next.val)) {
ListNode next = head.next;
head.next = p.next;
p.next = head;
head = next;
break;
}
p = p.next;
}
}
return fakeHead.next;
}
}
3.
class Solution {
public:
ListNode* reverse(ListNode* head) {
ListNode *curr = head, *prev = nullptr;
while (curr != nullptr) {
ListNode* next_node = curr->next;
Department of Computer Science and EngineeringPage 3
curr->next = prev;
prev = curr;
curr = next_node;
}
… }
if (carry > 0) {
curr->next = new ListNode(carry);
}
return reverse(dummy->next);
}
};
4.