[{"data":1,"prerenderedAt":236},["ShallowReactive",2],{"\u002Fblog\u002Fcode-log-day-4":3},{"id":4,"title":5,"author":6,"body":7,"date":222,"description":223,"draft":224,"extension":225,"image":226,"language":227,"meta":228,"navigation":140,"path":229,"seo":230,"stem":231,"tags":232,"__hash__":235},"blog\u002Fblog\u002FCode Log - Day 4.md","Code Log - Day 4","Caio Prado",{"type":8,"value":9,"toc":217},"minimark",[10,15,19,24,27,43,46,213],[11,12,14],"h2",{"id":13},"log","Log",[16,17,18],"p",{},"Today i only solved a LeetCode exercise.",[20,21,23],"h3",{"id":22},"leetcode","LeetCode",[16,25,26],{},"The LeetCode exercise i chose today was: Remove Duplicates from Sorted List.\nIt requires a function that receives the head of a linked list as input to remove the duplicates and return the list sorted:",[28,29,30],"blockquote",{},[16,31,32,33,37,38,42],{},"Given the ",[34,35,36],"code",{},"head"," of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list ",[39,40,41],"strong",{},"sorted"," as well.",[16,44,45],{},"To achieve this, i simply went through the linked list verifying if the current node had the same value of the next node, and, if they had the same value, the current node next node would now be the next node of the next node. Since i'm using java, i didn't need to free the memory occupied by the removed node because the garbage collector takes care of it.",[47,48,53],"pre",{"className":49,"code":50,"language":51,"meta":52,"style":52},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u002F**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n *\u002F\nclass Solution {\n    public ListNode deleteDuplicates(ListNode head) {\n        if (head == null) return head;\n\n        ListNode aux = head;\n        while(head.next != null){\n            if(head.val == head.next.val){\n                head.next = head.next.next;\n            } else {\n                head = head.next;\n            }\n        }\n\n        return aux;\n    }\n}\n","java","",[34,54,55,63,69,75,81,87,93,99,105,111,117,123,129,135,142,148,154,160,166,172,178,184,190,195,201,207],{"__ignoreMap":52},[56,57,60],"span",{"class":58,"line":59},"line",1,[56,61,62],{},"\u002F**\n",[56,64,66],{"class":58,"line":65},2,[56,67,68],{}," * Definition for singly-linked list.\n",[56,70,72],{"class":58,"line":71},3,[56,73,74],{}," * public class ListNode {\n",[56,76,78],{"class":58,"line":77},4,[56,79,80],{}," *     int val;\n",[56,82,84],{"class":58,"line":83},5,[56,85,86],{}," *     ListNode next;\n",[56,88,90],{"class":58,"line":89},6,[56,91,92],{}," *     ListNode() {}\n",[56,94,96],{"class":58,"line":95},7,[56,97,98],{}," *     ListNode(int val) { this.val = val; }\n",[56,100,102],{"class":58,"line":101},8,[56,103,104],{}," *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",[56,106,108],{"class":58,"line":107},9,[56,109,110],{}," * }\n",[56,112,114],{"class":58,"line":113},10,[56,115,116],{}," *\u002F\n",[56,118,120],{"class":58,"line":119},11,[56,121,122],{},"class Solution {\n",[56,124,126],{"class":58,"line":125},12,[56,127,128],{},"    public ListNode deleteDuplicates(ListNode head) {\n",[56,130,132],{"class":58,"line":131},13,[56,133,134],{},"        if (head == null) return head;\n",[56,136,138],{"class":58,"line":137},14,[56,139,141],{"emptyLinePlaceholder":140},true,"\n",[56,143,145],{"class":58,"line":144},15,[56,146,147],{},"        ListNode aux = head;\n",[56,149,151],{"class":58,"line":150},16,[56,152,153],{},"        while(head.next != null){\n",[56,155,157],{"class":58,"line":156},17,[56,158,159],{},"            if(head.val == head.next.val){\n",[56,161,163],{"class":58,"line":162},18,[56,164,165],{},"                head.next = head.next.next;\n",[56,167,169],{"class":58,"line":168},19,[56,170,171],{},"            } else {\n",[56,173,175],{"class":58,"line":174},20,[56,176,177],{},"                head = head.next;\n",[56,179,181],{"class":58,"line":180},21,[56,182,183],{},"            }\n",[56,185,187],{"class":58,"line":186},22,[56,188,189],{},"        }\n",[56,191,193],{"class":58,"line":192},23,[56,194,141],{"emptyLinePlaceholder":140},[56,196,198],{"class":58,"line":197},24,[56,199,200],{},"        return aux;\n",[56,202,204],{"class":58,"line":203},25,[56,205,206],{},"    }\n",[56,208,210],{"class":58,"line":209},26,[56,211,212],{},"}\n",[214,215,216],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":52,"searchDepth":65,"depth":65,"links":218},[219],{"id":13,"depth":65,"text":14,"children":220},[221],{"id":22,"depth":71,"text":23},"2025-01-10","Solving LeetCode's Remove Duplicates from Sorted List in Java.",false,"md",null,"en",{},"\u002Fblog\u002Fcode-log-day-4",{"title":5,"description":223},"blog\u002FCode Log - Day 4",[233,234,22],"challenge","daily-coding","B4fs8mPA0CY6jBQs6mpBCF4ntXu5J6Y0Wij5s7WJPNU",1785357636359]