1
+ //Problem Number : 19 - Leetcode
2
+
3
+ /**
4
+ * Definition for singly-linked list.
5
+ * public class ListNode {
6
+ * int val;
7
+ * ListNode next;
8
+ * ListNode() {}
9
+ * ListNode(int val) { this.val = val; }
10
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
11
+ * }
12
+ */
13
+
14
+ /*
15
+ * Problem Description :
16
+ * Given the head of a linked list, remove the nth node
17
+ * from the end of the list and return its head.
18
+ *
19
+ * linkedlist = 1 -> 2 -> 3 -> 4 -> 5-> null
20
+ * n = 2
21
+ */
22
+
23
+ /*
24
+ * Approach we follow to solve this Problem.
25
+ * Given : head of linkedlist and n = number of node to remove from end
26
+ * Solution :
27
+ * => Since we have to remove node from end , we need to calculate length of
28
+ * linkedlist.
29
+ * To do so lets create a function calcLength() , it take head node as parameter
30
+ * and return length of linked list .
31
+ * => Store the length of linkedlist in variable namely (len) by calling
32
+ * function calcLength().
33
+ * => To make it simple , lets calculate position of node from start of
34
+ * linkedlist by subtracting length of node with nth node to remove.
35
+ * and store the result in another variable called target. [ target = len - n]
36
+ * => Now, we check for target == 0 and if its true than we have to remove our
37
+ * head and point the head to next element .
38
+ * for ex : head.next = head
39
+ * After just return our head because we have no need to check other part.
40
+ * => create a pointer = 1 and store head of LikedList to temp variable .
41
+ * => After that , we have to iterate over linkedlist till our temp is not equal
42
+ * to null to find our target element.
43
+ * (Note : target element is always prev of node of node to remove for ex : 1-> 2-> 3->4-> 5
44
+ * In this case 3 is our target because we have to remove 4.)
45
+ * => for each iteration , we check if our target == pointer , In case its true
46
+ * the we have to handle two case :
47
+ * Case 1 : if our target node is last node of linked list ,then point temp to
48
+ * null
49
+ * explanation : 1-> 2-> 3-> 4-> 5 -> null
50
+ * if our target is 5 the our new linkedlist will we like this : 1-> 2-> 3-> 4->
51
+ * null
52
+ * Case 2 : Target is in middle of linkedlist then , update temp pointer to
53
+ * temp.next.next
54
+ * explanation : 1-> 2-> 3-> 4-> 5 -> null
55
+ * let say we have to remove 3 : then temp = 2
56
+ * temp.next = 3 : node to remove
57
+ * temp.next.next = 4
58
+ * output : 1-> 2-> 4-> 5 -> null
59
+ * => increment pointer and update temp to next node.
60
+ * => In last just return node
61
+ */
62
+
63
+ class Solution {
64
+
65
+ public int calcLength (ListNode head ) {
66
+ int cnt = 0 ;
67
+ while (head != null ) {
68
+ cnt ++;
69
+ head = head .next ;
70
+ }
71
+ return cnt ;
72
+ }
73
+
74
+ public ListNode removeNthFromEnd (ListNode head , int n ) {
75
+
76
+ int len = calcLength (head );
77
+ int target = len - n ;
78
+ if (target == 0 ) {
79
+ head = head .next ;
80
+ return head ;
81
+ }
82
+ int pointer = 1 ;
83
+
84
+ ListNode temp = head ;
85
+ while (temp != null ) {
86
+ if (pointer == target ) {
87
+ ListNode key = temp .next ;
88
+ if (key == null ) {
89
+ temp = null ;
90
+ } else {
91
+ temp .next = key .next ;
92
+ }
93
+
94
+ }
95
+ pointer ++;
96
+ temp = temp .next ;
97
+ }
98
+
99
+ return head ;
100
+
101
+ }
102
+ }
0 commit comments